C++ C++;为什么我的数组没有弹出到堆栈中?
我为studentrecords创建了一个数组,应该将它弹出到我的堆栈中。。除了我的stack.pops和stack.push外,所有的东西都能正常工作……我已经快完成这个程序了,我想知道是否有人知道任何解决方案C++ C++;为什么我的数组没有弹出到堆栈中?,c++,arrays,stack,C++,Arrays,Stack,我为studentrecords创建了一个数组,应该将它弹出到我的堆栈中。。除了我的stack.pops和stack.push外,所有的东西都能正常工作……我已经快完成这个程序了,我想知道是否有人知道任何解决方案 #include <iostream> #include <list> #include <cstdlib> #include <cstring> #include <iomanip> #include <string&
#include <iostream>
#include <list>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
using namespace std;
class Studentrecords
{
private:
struct student
{
string name;
string address;
int ID;
double gpa;
};
student *stackArray;
int stackSize;
int top;
public:
Studentrecords();
Studentrecords(int size);
~Studentrecords();
void push(string name, string address, int id, double gpa);
void pop();
bool isFull() const;
bool isEmpty() const;
void display();
};
Studentrecords::Studentrecords(int size)
{
stackArray = new student[size];
top = -1;
}
Studentrecords::Studentrecords()
{
stackSize = 20;
stackArray = new student[stackSize];
top = -1;
}
Studentrecords::~Studentrecords()
{
delete [] stackArray;
}
void Studentrecords::push (string name, string address, int id, double gpa)
{
if (isFull())
{
cout << "The stack is full!" << endl;
}
else
{
student newStudent;
newStudent.name = name;
newStudent.address= address;
newStudent.ID = id;
newStudent.gpa = gpa;
stackArray[top] = newStudent;
top++;
}
}
void Studentrecords::pop ()
{
if (isEmpty())
{
cout << "The stack is empty!" << endl;
}
else
{
cout << stackArray[top-1].name << endl;
cout << stackArray[top-1].address << endl;
cout << stackArray[top-1].ID << endl;
cout << stackArray[top-1].gpa << endl;
top--;
}
}
bool Studentrecords::isFull() const
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
bool Studentrecords::isEmpty() const
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
void Studentrecords::display()
{
for (int i = 0; i< top; i++)
{
cout << stackArray[i].name << endl;
cout << stackArray[i].address << endl;
cout << stackArray[i].ID << endl;
cout << stackArray[i].gpa << endl << endl;
}
}
int main()
{
int catchVar;
Studentrecords stack();
cout << "Pushing 1st";
stack.push("Jonny", "123 ave", 2343, 3.2);
cout << "pushing 2nd";
stack.push("Robby", "123 ave", 2343, 3.2);
cout << "Popping ";
stack.pop(catchVar);
cout << catchVar << endl;
cout << "Popping ";
stack.pop(catchVar);
cout << catchVar << endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级学生记录
{
私人:
结构学生
{
字符串名;
字符串地址;
int-ID;
双gpa;
};
学生*stackArray;
整数堆栈大小;
int top;
公众:
Studentrecords();
导线(内部尺寸);
~Studentrecords();
无效推送(字符串名称、字符串地址、整数id、双gpa);
void pop();
bool isFull()常量;
bool isEmpty()常量;
void display();
};
Studentrecords::Studentrecords(整数大小)
{
stackArray=新学员[大小];
top=-1;
}
Studentrecords::Studentrecords()
{
堆栈大小=20;
stackArray=新学员[stackSize];
top=-1;
}
Studentrecords::~Studentrecords()
{
删除[]堆栈数组;
}
void Studentrecords::push(字符串名称、字符串地址、int-id、双gpa)
{
如果(isFull())
{
库特
不声明名为堆栈的Studentrecords
,它声明名为堆栈的函数,该函数返回Studentrecords
。将其更改为
Studentrecords stack;
此外,您的类至少需要一个复制构造函数和赋值运算符
不声明名为堆栈的Studentrecords
,它声明名为堆栈的函数,该函数返回Studentrecords
。将其更改为
Studentrecords stack;
此外,您的类至少需要一个复制构造函数和赋值运算符。您能发布编译器的错误吗?
或者生产的产量与预期的产量?
如果没有这些,我不得不说你的pop函数没有参数,你正在传递它catchVar…那将是一个编译器错误。你能发布编译器的错误吗?
或者生产的产量与预期的产量?
如果没有这一点,我将不得不说您的pop函数不接受参数,而您正在传递它catchVar…这将是一个编译器错误。这段代码甚至可以编译吗?您正在调用pop(catchVar)
但是pop()
不接受任何参数。@GregHewgill不,它不可能编译。你可能想处理缩进,并将代码缩减到相关部分。你可能还想提到问题的确切原因。在不知道自己在找什么的情况下,费力地处理缩进程度低于设计要求的代码并不是很有趣……你有点放弃了ot来告诉我们问题是什么。仅仅告诉我们有些东西不起作用是没有多大帮助的。把它想象成去看一个机械师——你会说“我的车不起作用了”吗?或者你会告诉他你到底在做什么,到底出了什么问题吗?这段代码甚至可以编译吗?你正在调用pop(catchVar)
但是pop()
不接受任何参数。@GregHewgill不,它不可能编译。你可能想处理缩进,并将代码缩减到相关部分。你可能还想提到问题的确切原因。在不知道自己在找什么的情况下,费力地处理缩进程度低于设计要求的代码并不是很有趣……你有点放弃了我不想告诉我们问题出在哪里。只是告诉我们有些东西坏了并没有多大帮助。想想看,就像去看机械师一样——你能说“我的车坏了”吗或者你会告诉他你到底在做什么,到底出了什么问题吗?顺便说一句,clang++
为这个最麻烦的解析提供了一个非常明确的警告。可惜GCC没有。或者Studentrecords堆栈{}如果你有一个C++11compiler@Pr虽然ToRANN是不必要的,但最好还是把它们丢掉。在这种情况下,但是如果学生记录 是一个POD怎么办?”凯文巴拉德说:“不幸的是,大多数学习C++的人在一所大学上用一个非常古老的编译器或者在家里使用Windows,他们没有机会使用CLANG。总之,clang++
为这个最麻烦的解析提供了一个非常明确的警告。可惜GCC没有。或者Studentrecords堆栈{}如果你有一个C++11compiler@Pr虽然ToRANN是不必要的,但最好还是把它们丢掉。在这种情况下,但是如果学生记录 是一个POD怎么办?“凯文巴拉德。不幸的是,大多数学习C++的人在一所大学上用一个非常古老的编译器或者在家里使用Windows,他们没有机会使用CLANG。