C++怎么自定义一个输出结构体的函数

2024-11-13 22:49:52
推荐回答(3个)
回答1:

可以如下定义:
template
class mix
{
public:
mix();//<>是实例化的时候才用得。
void sort_all();
void out();
private:
struct unit
{
T x;
unit *next;
}
static void del_p( unit *p);
//此处省略部分成员
};
但是出于程序可读性的考虑,还是定义在类的外边比较好,如下:
struct Student{
char number[20];
char name[20];
float math;
float english;
float history;
};
class a{
private:
struct Student stu;
}
  如果在结构体定义时,或定义后取了别名,可以用别名,否则不能省“struct”
如:typedef struct Teacher TEACHER;
则可以用TEACHER 代替struct Teacher
C语言的结构体没有存取控制权限,相当于C++存取控制权限中的public:

回答2:

#include
using namespace std;
struct album
{
string name;
int age;
};//必须打分号
void putstruct(struct album t)//结构体类型的t,“struct'可以省略
{
cout< cout<}

int main()
{
album CV={"小刚",20};
putstruct(CV);
return 0;
}

回答3:

结构定义少了分号,而且结构体作为参数调用时,调用错误,修改如下:

#include

using namespace std;

struct album
{
string name;
int age;
};

void putstruct(album a)
{
cout< cout<}

int main()
{
album cv;
cv={"小光",20};
putstruct(cv);

return 0;
}