C++ 在另一个类中使用类函数
我有一个LinkedList.h头文件,它运行得非常好。我正在尝试使用LinkedList来实现堆栈。问题在于如何在此MyStack类中使用LinkedList类中的函数。InsertFront(键)是LinkedList类中的一个函数。现在,当我运行它时,我得到以下两个错误 “列表”:未声明的标识符 “.InsertFront”左侧必须具有类/结构/联合 这两项均指以下代码的最后一行:C++ 在另一个类中使用类函数,c++,class,linked-list,stack,header-files,C++,Class,Linked List,Stack,Header Files,我有一个LinkedList.h头文件,它运行得非常好。我正在尝试使用LinkedList来实现堆栈。问题在于如何在此MyStack类中使用LinkedList类中的函数。InsertFront(键)是LinkedList类中的一个函数。现在,当我运行它时,我得到以下两个错误 “列表”:未声明的标识符 “.InsertFront”左侧必须具有类/结构/联合 这两项均指以下代码的最后一行: #include "LinkedList.h" #include <iostream> usin
#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);
}
#包括“LinkedList.h”
#包括
使用名称空间std;
类MyStack
{
私人:
//链接列表;
公众:
//执行以下功能,其目的应显而易见
MyStack();
~MyStack();
无效推送(int键);
int-Pop();
bool是空的();
int Size();
void Display();//只显示从堆栈顶部到底部的元素,而不删除它们
int Top();//返回堆栈顶部的元素而不删除它
};
MyStack::MyStack()
{
链接列表;
}
MyStack::~MyStack()
{
}
void MyStack::Push(int键)
{
列表。插入前(键);
}
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;
}
#包括“MyStack.h”
#包括
使用名称空间std;
int main()
{
MyStack;
int值,选择;
做{
库特值;
s、 推(值);
打破
案例2:
你可以注释掉你的LinkedList
的声明,而将其作为构造函数的局部变量。当你完成构造函数后,当LinkedList列表超出范围时,类的其他成员应该如何知道呢?你注释掉了你的LinkedList
并将其作为构造函数的局部变量。当您完成构造函数后,类的其他成员如何知道链接列表
超出范围时?您的链接列表成员被注释掉。您的链接列表成员被注释掉。我注释掉了它,因为当我取消注释它时,我得到了很多信息错误,当我单击它们时,它显示系统找不到指定的文件。一些错误:1>c:\users\vaio\documents\downloads\ds\assignment2\linkedlist.h(155):警告C4715:“linkedlist::Read”:并非所有控制路径都返回值1>c:\users\vaio\documents\downloads\ds\assignment2\linkedlist.h(182):警告C4715:'LinkedList::Find':并非所有控制路径都返回值1>usestack.obj:错误LNK2019:未解析的外部符号“public:int u thiscall MyStack::Size(void)”(?Size@MyStack@@QAEHXZ)在函数_main中引用,您需要取消注释并修复这些错误,因为如果没有该行,您的代码将不知道您要查找的LinkedList是什么。您发布的两个错误位于LinkedList.h
,您尚未发布。另一个简单地说您从未定义int Size()
函数在你的MyStack
类中。那么我应该在构造函数中添加什么呢?在不知道具体实现的情况下,我不确定。不过,现在看起来你不需要任何东西。我对它进行了注释,因为当我取消注释它时,会出现很多错误,当我单击它们时,它会说系统找不到指定的文件。某些错误:1>c:\users\vaio\documents\downloads\ds\assignment2\linkedlist.h(155):警告C4715:“linkedlist::Read”:并非所有控制路径都返回值1>c:\users\vaio\documents\downloads\ds\assignment2\linkedlist.h(182):警告C4715:'LinkedList::Find':并非所有控制路径都返回值1>usestack.obj:错误LNK2019:未解析的外部符号“public:int u thiscall MyStack::Size(void)”(?Size@MyStack@@QAEHXZ)在函数_main中引用,您需要取消注释并修复这些错误,因为如果没有该行,您的代码将不知道您要查找的LinkedList是什么。您发布的两个错误位于LinkedList.h
,您尚未发布。另一个简单地说您从未定义int Size()
函数在您的MyStack
类中。那么我应该在构造函数中放入什么呢?在不知道具体实现的情况下,我不确定。不过,目前您似乎不需要任何东西。