在另一个类中使用类函数



我有一个LinkedList.h头文件运行完美。我正试图使用LinkedList来实现堆栈。问题是在这个MyStack类中使用LinkedList类的函数。InsertFront(key)是LinkedList类中的一个函数。现在,当我运行它时,我得到以下两个错误:

"列表":未说明的标志符

>>>>>>>InsertFront'必须有class/struct/union

这两个都指向下面代码的最后一行:

#include "LinkedList.h"
#include <iostream>
using namespace std;

class MyStack
{
    private:
    //LinkedList list;

    public:
        //Implement the following functions whose purpose should be obvious
        MyStack();
        ~MyStack();
        void Push(int key);
        int Pop();
        bool isEmpty();
        int Size();
        void Display(); // Just show the elements from top of stack to the bottom without removing them
        int Top(); // Return the element at the top of the stack without removing it
};

MyStack::MyStack()
{
    LinkedList list;
}
MyStack::~MyStack()
{
}
void MyStack::Push(int key)
{
    list.InsertFront(key);
}

cpp文件如下所示:

#include "MyStack.h"
#include <iostream>
using namespace std;
int main()
{
    MyStack s;
    int value, choice;
    do{
        cout << "Enter 1 to push,n2 to pop,n3 to display the contents of the stack,n4 to display the item at the top of the stack,n5 to find the size of the queue,n0 to quit"<<endl;
        cin >> choice;
        switch(choice)
        {
            case 1:
                cout << "Key: ";
                cin >> value;
                s.Push(value);
                break;
            case 2:
                cout << "The item removed from the top of the stack is: " << s.Pop() << endl;
                break;
            case 3:
                s.Display();
                break;
            case 4: 
                cout << "The item at the top of the stack is: " << s.Top() << endl;
                break;
            case 5: 
                cout << "The size of the stack is: " << s.Size() << endl;;
            case 0:
                break;
        }
    } while (choice != 0);
    return 0;
}

您注释掉了LinkedList的声明,而将其作为构造函数的局部变量。当您完成构造函数时,当LinkedList list超出作用域时,类的其余部分应该如何知道它?

相关内容

  • 没有找到相关文章