试图创建我的类c++的新对象时链接错误
Linking error when trying to create new object of my class C++
我试图通过创建SingleLinkedList的对象SingleLinkedList m1;
来测试我的程序。如果我不创建和操作对象,它会编译得很好。下面是我的main:
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文件,也没有工作。下面是我的。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
您没有告诉编译器您的类的。cpp文件。你需要输入g++ PocketOfMagic.cpp pocketmaindriver.cpp -o my_program
相关文章:
- 对象错误地显示为引用?
- 调用带有指针对象错误的指针变量
- 为什么谷歌测试/模拟显示 std::unique_ptr 泄露的模拟对象错误?
- C 数组指向对象错误
- C++:对象错误的多重定义
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- C 字符串对象错误字符
- 声明 Windows API 结构 (DCB) 的对象 - 错误 C4430:缺少类型说明符 - 假定为 int
- 无法调用成员函数而不出现对象错误
- C++指针/对象错误
- C++排序矢量对象错误
- C++:在映射中存储类对象(错误:获取类型为"Node"的临时对象的地址)
- 使用三元运算符初始化类对象错误
- 帧缓冲区对象错误
- 实例化新对象 - 错误
- C++打印出对象错误
- 如何更正类型对象错误
- 简单指针对象错误C++
- 运算符重载添加两个对象错误
- c++列表对象错误