$我有以下错误: 44 E:\ Assignment 2.cpp'std::cout newstack::push(x)'中'operator $我正在使用链表将数字放入包含结构的类堆栈中。包含这些数字的堆栈被打印出来。但是错误不会允许我将它们打印出来。 #include <iostream>using namespace std;typedef int itemType; class newstack {private: Using a struct of nodes which contains a item and pointer to the next node. struct node { itemType item; node *next; }; node *top; //pointer to the top node. public: void push(itemType newItem); //adds items to newstack }; int main() { newstack number; int x; cout<< "Number Stack" <<endl; for (x=0;x<=9;x++) cout<<(number).push(x)<<endl; //ERROR LINE //Takes 9 integers and adds them to the stack by printing them out. return 0; } void newstack::push(itemType newItem) // Precondition: Stack is empty. //Postcondition: Stack contains a itemType at the top and list or stack implements by 1. { if(top!=NULL) node *newTop; newTop=new node; (*newTop).item=newItem; (*newTop).next=top; top=newTop; } 最佳答案 在push()函数中,您将返回Void。但是您尝试编写函数返回的值到命令提示符-更改您的推送功能返回值或cout 进一步:您需要为您的newstack类重载newstack obj;cout<<obj;<<仅对内置数据类型(而不是自定义类类型)过载。您需要像这样重载它:std::ostream& operator<<(std::ostream& os, const newstack& obj);关于c++ - 我使用链接列表在堆栈上出错。无法打印数字循环。 (C++),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7958796/
10-11 23:22