每次我在头文件中有一个类并且我在源文件中使用该类时,都会遇到相同的错误。不管是哪个类(class)或哪个项目。
我正在尝试将一个新节点插入到链表数据结构的头部。
现在我有一个非常简单的头文件 main.h
:
namespace linkedlistofclasses {
class Node {
public:
Node();
Node(int value, Node *next);
//Constructor to initialize a node
int getData() const;
//Retrieve value for this node
Node *getLink() const;
//Retrieve next Node in the list
void setData(int value);
//Use to modify the value stored in the list
void setLink(Node *next);
//Use to change the reference to the next node
private:
int data;
Node *link;
};
typedef Node* NodePtr;
}
我的源文件
main.cpp
看起来像这样:#include <iostream>
#include "main.h"
using namespace std;
using namespace linkedlistofclasses;
void head_insert(NodePtr &head, int the_number) {
NodePtr temp_ptr;
//The constructor sets temp_ptr->link to head and
//sets the data value to the_number
temp_ptr = new Node(the_number, head);
head = temp_ptr;
}
int main() {
NodePtr head, temp;
//Create a list of nodes 4->3->2->1->0
head = new Node(0, NULL);
for (int i = 1; i < 5; i++) {
head_insert(head, i);
}
//Iterate through the list and display each value
temp = head;
while (temp !=NULL) {
cout << temp->getData() << endl;
temp = temp->getLink();
}
//Delete all nodes in the list before exiting
//the program.
temp = head;
while (temp !=NULL) {
NodePtr nodeToDelete = temp;
temp = temp->getLink();
delete nodeToDelete;
}
return 0;
}
我的问题是我得到了这些编译错误:
Undefined symbols for architecture x86_64:
"linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
head_insert(linkedlistofclasses::Node*&, int) in main.o
_main in main.o
"linkedlistofclasses::Node::getData() const", referenced from:
_main in main.o
"linkedlistofclasses::Node::getLink() const", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
如果我在不使用类的情况下运行代码,则将所有内容写入源文件
main.cpp
, 没有问题。但是无论我如何编写一个类,我总是会得到这个错误的一些变体。
最佳答案
您只是在声明以下类方法:
Node(int value, Node *next);
//Constructor to initialize a node
int getData() const;
//Retrieve value for this node
Node *getLink() const;
//Retrieve next Node in the list
void setData(int value);
//Use to modify the value stored in the list
void setLink(Node *next);
//Use to change the reference to the next node
其中允许包含的人main.h 看到的存在|类链接列表::节点 ,以及它的公众 成员。这样,您就可以调用它们,例如,从 main() 函数 .
但是,as they are simply declared and not defined ,这会在以后引发问题:
**Undefined symbols for architecture x86_64:
"linkedlistofclasses::Node::Node(int, linkedlistofclasses::Node*)", referenced from:
head_insert(linkedlistofclasses::Node*&, int) in main.o
_main in main.o
"linkedlistofclasses::Node::getData() const", referenced from:
_main in main.o
"linkedlistofclasses::Node::getLink() const", referenced from:
_main in main.o**
您收到的这些错误消息是因为 linker不知道与这些方法名称关联的代码所在的程序内部表示中的区域。而且找不到是因为你一开始没有定义它!考虑一下这种情况:调用一个代码不存在的函数有什么意义?
我建议你创建一个 Node.h 和 Node.cpp 文件,第一个带有声明,第二个带有定义( again, differences between the two concepts ),然后是 #include Node.h from main.c ?
在当前情况下,您会注意到,如果您另外调用任何这些:
void setData(int value);
//Use to modify the value stored in the list
void setLink(Node *next);
//Use to change the reference to the next node
他们的名字将被添加到您已经收到的消息错误中!
另外,为了提高可读性,我建议将命名空间更改为 LinkedListOfClasses ?
编辑:关于您对您在 pastebin 中发布的内容的评论:
// Node.h
namespace LinkedListOfClasses {
class Node {
public:
Node();
Node(int value, Node *next);
int getData() const;
Node *getLink() const;
void setData(int value);
void setLink(Node *next);
private:
int data;
Node *link;
};
typedef Node* NodePtr;
}
上面的代码包含声明 您在评论中提到的方法。关于丢失的定义 在 Node 类中声明的以下方法:
int getData() const;
Node *getLink() const;
您希望将它们的定义包含在您的 Node**.cpp** 文件中,而不是 中。 .h ! Node.cpp 将如下所示:
// Node.cpp
#include "Node.h"
using namespace LinkedListOfClasses;
void head_insert(NodePtr &head, int the_number) {
NodePtr temp_ptr;
temp_ptr = new Node(the_number, head);
head = temp_ptr;
}
Node::Node(int value, Node *next) {
}
void Node::setData(int value) {
}
void Node::setLink(Node *next) {
}
int Node::getData() const {
// getData definition was missing, now it's defined in Node.cpp!
}
Node *Node::getLink() const {
// getLink definition was missing, now it's defined in Node.cpp!
}
希望我能有所帮助。
关于C++,Xcode。每次我尝试使用类时都会出现同样的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872911/