包含 .cpp 文件时只能编译和 运行
Can only compile and run when including .cpp file
我正在用 C++ 进行一些链表练习,并为单链表创建了一个简单的 class。但是,当我尝试在主程序中包含头文件时,出现未定义的引用错误。如果我包含 .cpp 文件,它会按我想要的方式工作。
自从我上次用 C++ 编写代码以来已经有一段时间了,看在我的份上,我无法找出问题所在。一些帮助将不胜感激。我正在使用带有 git-bash 接口和 g++ -std=c++11 的 Windows Termnial。代码包含在下面!
//The main file .cpp
#include "SingleNode.h"
int main() {
SingleNode* tail = new SingleNode(2);
SingleNode* head = new SingleNode(1, tail);
head->print();
return 0;
}
//SingleNode.h
#ifndef SINGLENODE_H
#define SINGLENODE_H
class SingleNode {
public:
int val;
SingleNode* next;
SingleNode();
SingleNode(int x);
SingleNode(int x, SingleNode* next);
void print();
};
#endif
//SingleNode.cpp
#include "SingleNode.h"
#include <iostream>
using namespace std;
SingleNode::SingleNode() : val(0), next(nullptr) {}
SingleNode::SingleNode(int x) : val(x), next(nullptr) {}
SingleNode::SingleNode(int x, SingleNode* next) : val(x), next(next) {}
void SingleNode::print() {
if (this->next != nullptr) {
cout<<this->val<<"->";
this->next->print();
} else {
cout<<this->val<<"->"<<"null"<<endl;
}
}
当运行:
$ g++ -std=c++11 LinkedList.cpp -o LinkedList.exe
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x30): undefined reference to `SingleNode::SingleNode(int)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x59): undefined reference to `SingleNode::SingleNode(int, SingleNode*)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x69): undefined reference to `SingleNode::print()'
collect2.exe: error: ld returned 1 exit status
如果我改为 #include "SingleNode.cpp"
它工作正常。
你包含了头文件,所以原型是可用的,编译器不会报错。链接器需要找到与这些函数关联的源。使用 g++ -std=c++11 LinkedList.cpp -o LinkedList.exe
编译意味着您只使用主文件源,而不使用包含链表实现的其他文件。
解决办法是把SingleNode.cpp
文件也传给编译器。因此:
g++ -std=c++11 LinkedList.cpp SingleNode.cpp -o LinkedList.exe
并在 LinkedList.cpp
.
中包含 SingleNode.h
文件
#include
指令执行文本替换,这意味着当您包含 .cpp
文件(反过来包含头文件)时,您有一个最终的 translation unit包含两个源文件的所需来源。
另见:Why should I not include cpp files and instead use a header?
我正在用 C++ 进行一些链表练习,并为单链表创建了一个简单的 class。但是,当我尝试在主程序中包含头文件时,出现未定义的引用错误。如果我包含 .cpp 文件,它会按我想要的方式工作。
自从我上次用 C++ 编写代码以来已经有一段时间了,看在我的份上,我无法找出问题所在。一些帮助将不胜感激。我正在使用带有 git-bash 接口和 g++ -std=c++11 的 Windows Termnial。代码包含在下面!
//The main file .cpp
#include "SingleNode.h"
int main() {
SingleNode* tail = new SingleNode(2);
SingleNode* head = new SingleNode(1, tail);
head->print();
return 0;
}
//SingleNode.h
#ifndef SINGLENODE_H
#define SINGLENODE_H
class SingleNode {
public:
int val;
SingleNode* next;
SingleNode();
SingleNode(int x);
SingleNode(int x, SingleNode* next);
void print();
};
#endif
//SingleNode.cpp
#include "SingleNode.h"
#include <iostream>
using namespace std;
SingleNode::SingleNode() : val(0), next(nullptr) {}
SingleNode::SingleNode(int x) : val(x), next(nullptr) {}
SingleNode::SingleNode(int x, SingleNode* next) : val(x), next(next) {}
void SingleNode::print() {
if (this->next != nullptr) {
cout<<this->val<<"->";
this->next->print();
} else {
cout<<this->val<<"->"<<"null"<<endl;
}
}
当运行:
$ g++ -std=c++11 LinkedList.cpp -o LinkedList.exe
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x30): undefined reference to `SingleNode::SingleNode(int)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x59): undefined reference to `SingleNode::SingleNode(int, SingleNode*)'
C:\AppData\Local\Temp\ccEbmcRt.o:LinkedList.cpp:(.text+0x69): undefined reference to `SingleNode::print()'
collect2.exe: error: ld returned 1 exit status
如果我改为 #include "SingleNode.cpp"
它工作正常。
你包含了头文件,所以原型是可用的,编译器不会报错。链接器需要找到与这些函数关联的源。使用 g++ -std=c++11 LinkedList.cpp -o LinkedList.exe
编译意味着您只使用主文件源,而不使用包含链表实现的其他文件。
解决办法是把SingleNode.cpp
文件也传给编译器。因此:
g++ -std=c++11 LinkedList.cpp SingleNode.cpp -o LinkedList.exe
并在 LinkedList.cpp
.
SingleNode.h
文件
#include
指令执行文本替换,这意味着当您包含 .cpp
文件(反过来包含头文件)时,您有一个最终的 translation unit包含两个源文件的所需来源。
另见:Why should I not include cpp files and instead use a header?