C++、Xcode.每次我尝试使用类时都会出现同样的错误
C++, Xcode. Same error every time i try and use classes
每次我在头文件中有一个类,并且我在源文件中使用该类时,我都会得到相同的错误。不管是哪个类或哪个项目。
我正在尝试在链表数据结构的头上插入一个新节点。
现在我有一个非常简单的头文件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!
}
希望我能帮上点忙。
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 在 xcode 10 上找不到 stdlibc++ 标头错误
- '-print_statistics'链接器选项在 Xcode 中产生错误
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- 终端错误MacOS:/Applications/Xcode.app/Content/Developer/Toolchai
- C++ Mac Xcode 错误:控制可能达到非无效功能的末尾
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- OPENCV错误在XCode 8中加载Fisher面部识别器时
- Xcode 8.3.2 中的错误警告:文件的非可移植路径 - 指定的路径与磁盘上的文件名不同
- 将框架包括在Xcode C 项目中 - 链接器错误
- 当我尝试构建和运行C 程序时,获取Xcode错误代码
- xcode 上的 stl 链接错误
- 错误的同时将C 文件夹在iOS XCode项目中包括在内
- 链接错误:Xcode 5 的体系结构x86_64未定义引用
- 向C Primer学习.预期的表达式错误 Xcode
- 编译器错误 xcode 未声明标识符 C++
- Qt创建者-项目错误:Xcode设置不正确.您可能需要通过运行/usr/bin/xcodebuild来确认许可协议
- md5.h文件未找到错误Xcode 7 for Mac应用程序
- 使用未声明的标识符和预期的表达式错误.Xcode