简体   繁体   English

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

[英]Adding object to Linked List error [C++]

I am attempting to add an object to a Linked List structure but keep receiving this error in Visual Studio 2015: 我试图将对象添加到链接列表结构,但在Visual Studio 2015中不断收到此错误:

Error   LNK2019 unresolved external symbol "public: void __thiscall Stack::add(class Creature *)" (?add@Stack@@QAEXPAVCreature@@@Z) referenced in function _main    

Here is my code to add to the list - this functions fine if I modify it to simply add an integer value to a linked list(not allowed to use the STL): 这是我要添加到列表中的代码-如果我对其进行修改以仅向链接列表中添加整数值(不允许使用STL),则此功能很好:

#include "Creature.h"

void Stack::add(Creature* obj) {
    /* create head node if list is empty */
    if (head == NULL) {
        head = new Node;
        head->data = obj;
        head->next = NULL;
    }
    else {
        /* set pointer to head */
        Node* temp = head;

        /* iterate until next node is empty */
        while (temp->next != NULL)
            temp = temp->next;

        /* create new node when NULL */
        temp->next = new Node;
        temp->next->data = obj;
        temp->next->next = NULL;
    }
}

Here is my Creature class definition (abstract class): 这是我的Creature类定义(抽象类):

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;
};

And here is my main function where I attempt to add the object to the list: 这是我尝试将对象添加到列表的主要功能:

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

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

I am still pretty fresh to C++ so I am trying to learn everything that I can and attempt to figure things out on my own first before asking for help, but I just cannot see what I may be missing here. 我对C ++还是很新鲜,所以我试图学习所有可能的知识,并尝试在寻求帮助之前自行解决问题,但我只是看不到这里可能缺少的内容。 Any help/resources would be greatly appreciated! 任何帮助/资源将不胜感激!

The error LNK 2019 is because of "A function or variable is declared but not defined." LNK 2019错误是因为“已声明但未定义函数或变量”。 But as you mention in the above you have defined the stack::add definition. 但是正如您在上面提到的,您已经定义了stack :: add定义。 Then it might be not added to your current project and hence it will not find the definition. 然后,它可能不会添加到您的当前项目中,因此将找不到该定义。

In Visual Studio solution tree right click on the project then Add -> Existing item -> choose the source file (I guess in your case it is stack.cpp) 在Visual Studio解决方案树中,右键单击项目,然后单击添加->现有项->选择源文件(我想在您的情况下是stack.cpp)

看来我已经解决了,我继续前进,咬了一下子句,只是删除了项目,然后重新导入了文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM