struct TodoItem
{
std::string todo;
};
const int MAX_STACK_SIZE = 5;
class TodoStackArray
{
public:
TodoStackArray(); //confusion here<---
bool isEmpty();
bool isFull();
void push(std::string todoItem);
void pop();
TodoItem* peek();
/* for grading purposes we need these following methods */
int getStackTop() { return stackTop; }
TodoItem** getStack() { return stack; }
private:
int stackTop;
TodoItem* stack[MAX_STACK_SIZE];
};
Above, is some declarations and a class from a header file for a current assignment. Our duty was to implement a stack program using this header file. However, I am a little confused as to the purpose of the TodoStackArray()
in the Header File. Is this supposed to be a base constructor? Am I supposed to use it for anything??
上面是一些声明和来自当前作业的头文件的类。我们的职责是使用此头文件实现堆栈程序。但是,我对Header文件中的TodoStackArray()的目的有点困惑。这应该是一个基础构造函数吗?我应该用它做什么吗?
I understand that this is somewhat situation specific but given that stack Arrays are STL I figured you might all be able to provide some insight. Thanks!
我知道这有点特定情况,但鉴于堆栈数组是STL我认为你可能都能提供一些见解。谢谢!
In case you want to see what I did with this header file...
如果你想看看我对这个头文件做了什么......
TodoStackArray::TodoStackArray() //This, unsurprisingly, produces an error.
{
stackTop = -1;
stack[stackTop];
}
bool TodoStackArray::isEmpty()
{
return (stackTop == -1);
}
TodoItem* TodoStackArray::peek()
{
if(stackTop ==-1)
{
cout<< "Stack empty, cannot peak."<<endl;
}
else
{
return(stack[stackTop]);
}
}
bool TodoStackArray::isFull()
{
return(stackTop == 4);
}
void TodoStackArray::push(std::string todoItem)
{
if(stackTop >= 5)
{
cout<<"Stack full, cannot add new todo item."<<endl;
}
else
{
stack[stackTop++];
stack[stackTop]->todo = todoItem;
}
}
void TodoStackArray::pop()
{
if(stackTop == -1)
{
cout<<"Stack empty, cannot pop an item."<<endl;
}
else
{
stackTop--;
}
}
Also, to be clear, we were not provided driver software. They will be assessing from their own stuff so we have to write our own driver software to test our functions. Hence the lack of any Main implementation.
另外,要明确的是,我们没有提供驱动程序软件。他们将从他们自己的东西评估,所以我们必须编写自己的驱动程序软件来测试我们的功能。因此缺乏任何主要实施。
1 个解决方案
#1
1
Yes, TodoStackArray::TodoStackArray()
really is your class' default constructor.
是的,TodoStackArray :: TodoStackArray()确实是你的类的默认构造函数。
What you are supposed to do with it just as with any default constructor:
你应该用它做什么,就像任何默认构造函数一样:
How are the class members supposed to be initialized if I create a new
TodoStackArray
?如果我创建一个新的TodoStackArray,应该如何初始化类成员?
The answer depends on what your class is supposed to look like in its initial state.
答案取决于你的班级在初始状态下应该是什么样子。
In you situation, "by default" a stack is empty. Setting the stackTop
to -1 was not a bad idea and the rest of your implementation is consistent with that (stackTop == -1
clearly means "empty stack").
在您的情况下,“默认情况下”堆栈为空。将stackTop设置为-1并不是一个坏主意,其余的实现与此一致(stackTop == -1显然意味着“空堆栈”)。
The odd thing that happens in your constructor is this:
在你的构造函数中发生的奇怪的事情是这样的:
stack[stackTop];
What is, according to you, the purpose of this line? You are reading the value in your array at a negative index, and we all know this kind of things never end well.
根据你的说法,这条线的目的是什么?您正在以负索引读取数组中的值,我们都知道这种事情永远不会结束。
Constructors are made to initialize your data. Here you are reading something you didn't initialize at an index that doesn't exist. Doesn't make much sense, does it? :)
构造函数用于初始化数据。在这里,您正在阅读未在不存在的索引处初始化的内容。没有多大意义,是吗? :)
Just get rid of this line and you should be able to move on. Here is an equivalent implementation using member initializer list (which are considered nicer):
只是摆脱这条线,你应该能够继续前进。这是使用成员初始化列表的等效实现(被认为更好):
TodoStackArray::TodoStackArray() : stackTop(-1)
{
// notice: no body required here
}
Have fun!
玩的开心!
#1
1
Yes, TodoStackArray::TodoStackArray()
really is your class' default constructor.
是的,TodoStackArray :: TodoStackArray()确实是你的类的默认构造函数。
What you are supposed to do with it just as with any default constructor:
你应该用它做什么,就像任何默认构造函数一样:
How are the class members supposed to be initialized if I create a new
TodoStackArray
?如果我创建一个新的TodoStackArray,应该如何初始化类成员?
The answer depends on what your class is supposed to look like in its initial state.
答案取决于你的班级在初始状态下应该是什么样子。
In you situation, "by default" a stack is empty. Setting the stackTop
to -1 was not a bad idea and the rest of your implementation is consistent with that (stackTop == -1
clearly means "empty stack").
在您的情况下,“默认情况下”堆栈为空。将stackTop设置为-1并不是一个坏主意,其余的实现与此一致(stackTop == -1显然意味着“空堆栈”)。
The odd thing that happens in your constructor is this:
在你的构造函数中发生的奇怪的事情是这样的:
stack[stackTop];
What is, according to you, the purpose of this line? You are reading the value in your array at a negative index, and we all know this kind of things never end well.
根据你的说法,这条线的目的是什么?您正在以负索引读取数组中的值,我们都知道这种事情永远不会结束。
Constructors are made to initialize your data. Here you are reading something you didn't initialize at an index that doesn't exist. Doesn't make much sense, does it? :)
构造函数用于初始化数据。在这里,您正在阅读未在不存在的索引处初始化的内容。没有多大意义,是吗? :)
Just get rid of this line and you should be able to move on. Here is an equivalent implementation using member initializer list (which are considered nicer):
只是摆脱这条线,你应该能够继续前进。这是使用成员初始化列表的等效实现(被认为更好):
TodoStackArray::TodoStackArray() : stackTop(-1)
{
// notice: no body required here
}
Have fun!
玩的开心!