我没有经常发帖所以请原谅任何不良做法,或者如果这个问题在另一篇文章中得到解决,但我找不到任何内容。
我的问题是我有一个模板Stack类,当我用int测试它时工作正常,但我应该能够选择在每个堆栈中添加整数和学生。在主文件中,我有代码将学生或int添加到他们各自的堆栈中,具体取决于用户选择的内容,但编译器给出了错误,说我正在尝试将int添加到Student堆栈中学生到int堆栈。请帮忙!
这些是我得到的错误
main.cpp:44:21: error: invalid conversion from 'int' to 'const Students*' [-fpermissive]
stack->push(value);
^
In file included from main.cpp:2:0:
Stack.h:23:10: note: initializing argument 1 of 'void Stack::push(const DataType*) [with DataType = Students]'
void push(const DataType*);
---
main.cpp:44:21: error: invalid conversion from 'int' to 'const int*' [-fpermissive]
stack->push(value);
^
In file included from main.cpp:2:0:
Stack.h:23:10: note: initializing argument 1 of 'void Stack::push(const DataType*) [with DataType = int]'
void push(const DataType*);
这是我的代码
主:
#include <iostream>
#include "Stack.h"
#include "Students.h"
using namespace std;
int menu()
{
int response = 0;
cout << "Menu:" << endl;
cout << "1. Push a student to the stack" << endl;
cout << "2. Delete a student from the stack" << endl;
cout << "3. Print the top element of the stack" << endl;
cout << "4. Stack type selection" << endl;
cout << "Please input the option you would like: ";
cin >> response;
cout << "---------------------------------------" << endl;
return response;
}
template <class DataType>
bool validateInput(int response, Stack<DataType>* stack, bool isSStack)
/* returns a bool determining if the program should quit or not,
* true if the program should quit
*/
{
bool quit = false;
if(response >= 1 && response <= 5)
{
switch(response)
{
case 1:
{
if(isSStack)
{
Students* x = new Students();
stack->push(x); //this gives an error
}
else
{
int value = 0;
cout << "What would you like to add? : ";
cin >> value;
stack->push(value);//and this gives an error
}
//stack->push(x);
cout << string(100, '\n');
break;
}
case 2:
{
cout << string(100, '\n');
stack->pop();
break;
}
case 3:
{
cout << string(100, '\n');
//cout << "Top element is: " << x->topStack() << endl;
break;
}
case 4:
{
cout << "back";
quit = true;
break;
}
}//end switch
}
else
cout << string(100, '\n') << "Please enter a valid integer" << endl << endl;
return quit;
}
int main()
{
Stack<int>* iStack = new Stack<int>();
Stack<Students>* sStack = new Stack<Students>();
cout << "Choose a stack type" << endl << "0. Student" << endl << "1. Integer";// << end;
bool isSStack = 0;
cin >> isSStack;
cout << string(100, '\n');
bool quit = false;
while(!quit)//menu asks for which option, which returns the choice into validateInput, which returns if the program should quit
{
if(isSStack)
quit = validateInput(menu(), sStack, isSStack);
else
quit = validateInput(menu(), iStack, isSStack);
//stack->printStack();
}
return 0;
}
Stack.cpp
#include "Stack.h"
#include <iostream>
using namespace std;
template <class DataType>
Stack<DataType>::Stack(int size)
{
maxSize = size;
}
template <class DataType>
Stack<DataType>::~Stack()
{
if(top == NULL)
cout << "Stack is empty" << endl;
else
{
StackNode<DataType>* temp = top;
StackNode<DataType>* deleter = temp;
while(temp != NULL)//go until it finds null, we past the last node
{
deleter = temp;
temp = temp->next;//move temp down the stack
delete deleter; //delete the node we past
}
}
}
template <class DataType>
void Stack<DataType>::push(const DataType* d)
{
if(!isFull())
{
StackNode<DataType>* newNode= new StackNode<DataType>();
newNode->data = d;
newNode->next = top;
top = newNode;
numNodes++;
}else
cout << "Stack is full" << endl;
}
template <class DataType>
void Stack<DataType>::pop()
{
if(!isEmpty())
{
//cout << "pop" << endl;
StackNode<DataType>* deleter = top;
top = top->next;
delete deleter;
numNodes--;
}else
cout << "Stack is empty" << endl;
}
template <class DataType>
bool Stack<DataType>::isEmpty() const
{
return (numNodes <= 0);
}
template <class DataType>
bool Stack<DataType>::isFull() const
{
return (numNodes >= maxSize);
}
template <class DataType>
DataType Stack<DataType>::topStack() const
{
return top->data;
}
template <class DataType>
void Stack<DataType>::printStack()
{
if(!isEmpty())
{
StackNode<DataType>* temp = top;
//cout << "num " << numNodes << endl;
while(temp != NULL)
{
cout << temp->data << ", ";
temp = temp->next;
}
cout << endl;
}else
cout << "Empty stack" << endl;
}
编辑:
我添加了两个pushNew()函数来处理创建和推送到堆栈但现在我在这些方法中得到未定义的引用错误
void pushNew(Stack<int>* stack, int* x)
{
cout << "What would you like to add? : ";
cin >> *x;
stack->push(x);
}
void pushNew(Stack<Student>* stack, Student* x)
{
stack->push(x);
}