我正在学习与我学习堆栈的成员函数有问题。我不能使用模板,这就是我遇到问题的原因。我相信我似乎无法正确创建tempStack
。我尝试使用Element300
和Stack300
都给了我不同的编译器错误。如果我能得到一些很好的帮助。如果您发现任何其他错误,请告诉我。我的朋友遇到push
功能问题。由于我对tempStack
的问题,我无法帮助他。
标题文件:
typedef float Element300;
class Stack300
{
private:
const int MAX_STACK;
Element300 * stackArray;
int top;
public:
Stack300();//
Stack300(int);//
Stack300(Stack300 &);//
~Stack300();//
void push300(const Element300);//
Element300 pop300();//
void viewTB300();
void viewBT300();
bool isFull();//
bool isEmpty();//
};
会员职能:
using namespace std;
Stack300::Stack300(): MAX_STACK(3)
{
stackArray = new(std::nothrow) Element300[MAX_STACK];
if(stackArray == NULL)
{
cerr << "There is not enough memory for this stack. This stack will not be implemented correctly." << endl;
}
else
{
top = -1;
}
}
Stack300::Stack300(int size): MAX_STACK(size)
{
stackArray = new Element300[MAX_STACK];
top = -1;
}
Stack300::Stack300(Stack300 & old)
{
Element300 * tempStack;
int tempTop = -1;
Element300 tempValue;
int tempSize = old.MAX_STACK;
tempStack = new(std::nothrow) Element300[tempSize];
while(tempTop <= old.MAX_STACK)
{
tempValue = old.pop300();
tempStack.push300(tempValue);
}
while(top <= tempStack.MAX_STACK)
{
tempValue = tempStack.pop300();
old.push300(tempValue);
}
}
Stack300::~Stack300()
{
Element300 tempValue;
while(!isEmpty())
{
tempValue = pop300();
}
if(isEmpty())
{
delete [] stackArray;
}
}
void Stack300::push300(const Element300 number)
{
if(isFull())
{
cerr << "The stack is full." << endl << "The push was unsuccessful" << endl;
}
else
{
top++;
stackArray[top] = number;
}
return;
}
Element300 Stack300::pop300()
{
Element300 number = 0.0;
if(isEmpty())
{
cerr << "The stack is empty" << endl << "The pop was unsuccessful" << endl;
return 0.0;
}
else
{
number = stackArray[top];
top--;
return number;
}
}
void Stack300::viewTB300()
{
Element300 * tempStack = new (std::nothrow)Element300[MAX_STACK];
Element300 tempValue;
while(top > 0)
{
tempValue = pop300();
tempStack.push300(tempValue);
cout << tempValue << endl;
}
while(top < MAX_STACK)
{
tempValue = tempStack.pop300();
push300(tempValue);
}
return;
}
void Stack300::viewBT300()
{
Element300 * tempStack = new (std::nothrow)Element300[MAX_STACK];
Element300 tempValue;
while(top > 0)
{
tempValue = pop300();
tempStack.push300(tempValue);
}
while(top < MAX_STACK)
{
tempValue = tempStack.pop300();
cout << tempValue << endl;
push300(tempValue);
}
}
bool Stack300::isFull()
{
bool status;
if(top == MAX_STACK - 1)
{
status = true;
}
else
{
status = false;
}
return status;
}
bool Stack300::isEmpty()
{
bool status;
if(top == - 1)
{
status = true;
}
else
{
status = false;
}
return status;
}
这也是我得到的错误:
olsond2.cpp: In copy constructor ‘Stack300::Stack300(Stack300&)’:
olsond2.cpp:26: error: uninitialized member ‘Stack300::MAX_STACK’ with ‘const’ type ‘const int’
olsond2.cpp:38: error: request for member ‘push300’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp:41: error: request for member ‘MAX_STACK’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp:43: error: request for member ‘pop300’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp: In member function ‘void Stack300::viewTB300()’:
olsond2.cpp:101: error: request for member ‘push300’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp:106: error: request for member ‘pop300’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp: In member function ‘void Stack300::viewBT300()’:
olsond2.cpp:122: error: request for member ‘push300’ in ‘tempStack’, which is of non-class type ‘Element300*’
olsond2.cpp:126: error: request for member ‘pop300’ in ‘tempStack’, which is of non-class type ‘Element300*’