C++、Xcode.每次我尝试使用类时都会出现同样的错误

C++, Xcode. Same error every time i try and use classes

本文关键字:错误 Xcode C++      更新时间:2023-10-16

每次我在头文件中有一个类,并且我在源文件中使用该类时,我都会得到相同的错误。不管是哪个类或哪个项目。

我正在尝试在链表数据结构的头上插入一个新节点。

现在我有一个非常简单的头文件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()函数调用它们。

然而,由于它们只是简单地声明而没有定义,这在以后会引发问题:

**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**

出现这些错误消息是因为链接器不知道程序内部表示中与这些方法名称关联的代码所在的区域。它找不到,因为你一开始没有定义它!考虑一下这种情况:调用一个代码不存在的函数有什么意义?

我可以建议您创建一个Node.h和Node.cpp文件,第一个带有声明,第二个带有定义(同样,这两个概念之间的区别),然后#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!
}

希望我能帮上点忙。