gpt4 book ai didi

c++ - 显示包含多个类的链表?

转载 作者:行者123 更新时间:2023-11-30 02:02:09 24 4
gpt4 key购买 nike

我不确定为什么我的显示功能不起作用。我的 cout 声明是这样的

no match for operator << in std :: cout<<n->movieinventory::movienode::m

有什么想法吗?

class MovieInventory
{
private:
struct MovieNode // the Nodes of the linked list
{
Movie m; // data is a movie
MovieNode *next; // points to next node in list
};

MovieNode *movieList; // the head pointer

bool removeOne(Movie); // local func, used by sort and removeMovie

public:
MovieInventory();

bool addMovie(Movie);
int removeMovie(Movie);
void showInventory();
Movie findMinimum(); // should be private, but public for testing
void sortInventory();

int getTotalQuantity();
float getTotalPrice();

};

显示代码:

void MovieInventory::showInventory()
{

MovieNode *n;

for (n = movieList; n != NULL; n = n->next)
cout << n->m;
}

最佳答案

数据成员m属于Movie类(class)。 cout<< operator仅对内置数据类型(如 int、char、float 等)进行重载。因此它不会输出用户定义数据类型的对象。你必须重载 <<你自己的类(class)的运营商。

如果你不想重载运算符<<,你必须通过这种方式一个一个地输出Movie类的数据成员,前提是它们是公开声明的。

cout << n->m.var1 ;
cout << n->m.var2 ;

如果 Movie 类的数据成员是私有(private)的,则您必须为此创建 getter 函数。

cout << n->m.getvar1() ;
cout << n->m.getvar2() ;

关于c++ - 显示包含多个类的链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13548858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com