[英]Adding object to Linked List error [C++]
我試圖將對象添加到鏈接列表結構,但在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 "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;
}
}
這是我的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;
};
這是我嘗試將對象添加到列表的主要功能:
#include "Stack.h"
#include "Creature.h"
#include "Barbarian.h"
int main() {
Stack q;
Creature *test = new Barbarian;
q.add(test);
return 0;
}
我對C ++還是很新鮮,所以我試圖學習所有可能的知識,並嘗試在尋求幫助之前自行解決問題,但我只是看不到這里可能缺少的內容。 任何幫助/資源將不勝感激!
LNK 2019錯誤是因為“已聲明但未定義函數或變量”。 但是正如您在上面提到的,您已經定義了stack :: add定義。 然后,它可能不會添加到您的當前項目中,因此將找不到該定義。
在Visual Studio解決方案樹中,右鍵單擊項目,然后單擊添加->現有項->選擇源文件(我想在您的情況下是stack.cpp)
看來我已經解決了,我繼續前進,咬了一下子句,只是刪除了項目,然后重新導入了文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.