Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将对象添加到链接列表错误[C+;+;]_C++_Class_Data Structures_Linked List - Fatal编程技术网

C++ 将对象添加到链接列表错误[C+;+;]

C++ 将对象添加到链接列表错误[C+;+;],c++,class,data-structures,linked-list,C++,Class,Data Structures,Linked List,我正在尝试向链表结构添加对象,但在Visual Studio 2015中不断收到此错误: Error LNK2019 unresolved external symbol "public: void __thiscall Stack::add(class Creature *)" (?add@Stack@@QAEXPAVCreature@@@Z) referenced in function _main 下面是我要添加到列表中的代码-如果我将其修改为只向链接列表中添加一个整数值(不

我正在尝试向链表结构添加对象,但在Visual Studio 2015中不断收到此错误:

Error   LNK2019 unresolved external symbol "public: void __thiscall Stack::add(class Creature *)" (?add@Stack@@QAEXPAVCreature@@@Z) referenced in function _main    
下面是我要添加到列表中的代码-如果我将其修改为只向链接列表中添加一个整数值(不允许使用STL),则此函数可以正常工作:

这是我的生物类定义(抽象类):

这是我的主要功能,我尝试将对象添加到列表中:

#include "Stack.h"
#include "Creature.h"
#include "Barbarian.h"

int main() {
    Stack q;
    Creature *test = new Barbarian;
    q.add(test);
    return 0;
}
我对C++仍然很熟悉,所以我试着去学习我能做的每一件事,并试图在我寻求帮助之前先自己解决问题,但我就是看不到我可能错过了什么。任何帮助/资源都将不胜感激

错误是因为“函数或变量已声明但未定义”。但正如您在上面提到的,您已经定义了stack::add定义。然后它可能不会添加到当前项目中,因此它将找不到定义


在Visual Studio解决方案树中,右键单击项目,然后添加->现有项->选择源文件(我猜在您的示例中是stack.cpp)

看起来我解决了它,我继续努力,咬紧牙关,只是删除了项目并重新导入了文件。

您的代码是如何编译的?@BillLynch在visual studio中,我只是去“构建”选项卡,然后选择build solution…这就是您的意思吗?您收到的错误表明您没有正确地将所有源文件包含在二进制文件中。也许VisualStudio有它所采取行动的日志?
class Creature {
    protected:
        int strike, defense,
            armor, strength,
            damage;
        bool alive;
        string type;
    public:
        Creature(
                strike = 0;
                defense = 0;
                armor = 0;
                strength = 0;
                alive = true;
                type = " ";
                );
        virtual int attack() = 0;
        virtual bool defend(int) = 0;
        virtual string name() = 0;
};
#include "Stack.h"
#include "Creature.h"
#include "Barbarian.h"

int main() {
    Stack q;
    Creature *test = new Barbarian;
    q.add(test);
    return 0;
}