我试图通过创建SingleLinkedList的对象SingleLinkedList m1;
来测试我的程序。如果我不创建和操作对象,它编译得很好。这是我的主:尝试创建我的类的新对象时发生链接错误C++
int main()
{
SingleLinkedList m1;
displayMenu();
int user_menu;
cin >> user_menu; //get input from user which pocket they want to go to
while (user_menu != 4)
{
displayChoices();
int user_input; //
cin >> user_input;
if (user_input == 1) {
cout << "You selected the Pocket Of Magic!" << endl;
cout << "What do you choose to do?" << endl;
//string user_choice;
//cin >> user_choice;
//SingleLinkedList m1;
m1.operationMagic();
}
else if (user_input == 2) {
cout << "You selected the Pocket Of Potions!" << endl;
cout << "What do you choose to do?" << endl;
}
user_input = 4;
}
displayMenu();
然而,当我尝试链接,出现此错误:
Undefined symbols for architecture x86_64:
"SingleLinkedList::SingleLinkedList()", referenced from:
_main in pocketmaindriver-df9a60.o
"SingleLinkedList::~SingleLinkedList()", referenced from:
_main in pocketmaindriver-df9a60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
是什么原因呢?我试着包括.cpp文件以及和没有work.Here是我的.h文件进行比较:
#ifndef POCKET_OF_MAGIC_
#define POCKET_OF_MAGIC_
//using namespace std;
class SingleNode {
private:
std::string value;
SingleNode *nextNode;
public:
SingleNode();
SingleNode(std::string s);
std::string getValue();
SingleNode* getNextNode();
void setNextNode(SingleNode *newNode);
};
class SingleLinkedList {
private:
SingleNode *head;
SingleNode *tail;
int totalNodes;
int itemCount;
public:
SingleLinkedList();
~SingleLinkedList();
void traverseAndPrint();
void addNode(std::string data);
bool deleteNode(std::string data);
void operationMagic();
}; //end pocket of magic
#endif //end pocket of magic
它来自'link command'=>这是一个链接器错误。 – EJP
如果您有构建/链接错误,但可能会在您的代码部分中包含'#include's。 – Taekahn
查看您用来编译程序的命令会很有帮助。 –