在 C++ 中包含标头时未定义引用

发布于 2024-10-11 14:11:58 字数 1275 浏览 7 评论 0原文

当我决定应该将其拆分为文件时,我正在处理我的项目。然而,我遇到了这样的问题,我通过谷歌找到的所有建议都是关于忘记链接我做对的两个对象文件(至少我认为是这样)。

Makefile:

test : class.o main.o
 g++ class.o main.o -o test.exe

main.o : main.cpp
 g++ main.cpp -c

class.o : class.cpp
 g++ class.cpp -c

ma​​in.cpp

#include <iostream>
#include "class.h"
using namespace std;

int main() {
 Trida * t = new Trida(4);
 t->fce();
 return 0;
}

class.h

#ifndef CLASS
#define CLASS
class Trida {
private:
 int a; 
public:
 Trida(int n); 
 void fce();
};
#endif

class.cpp

#include <iostream>

using namespace std;

class Trida {
private:
 int a;

public:
 Trida(int n) {
  this->a = n;
 } 

 void fce() {
  cout << this->a << endl;
 }
};

错误消息:

gwynbleidd@gwynbleidd-pc:~/Skola/test$ make
g++ class.cpp -c
g++ main.cpp -c
g++ class.o main.o -o test.exe
main.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Trida::Trida(int)'
main.cpp:(.text+0x54): undefined reference to `Trida::fce()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

I was working on my project while I decided that I should split it into files. However I got stucked with problem like this and all advice I found via google were about forgetting to link both object files which I am doing right (at least I think so).

Makefile:

test : class.o main.o
 g++ class.o main.o -o test.exe

main.o : main.cpp
 g++ main.cpp -c

class.o : class.cpp
 g++ class.cpp -c

main.cpp

#include <iostream>
#include "class.h"
using namespace std;

int main() {
 Trida * t = new Trida(4);
 t->fce();
 return 0;
}

class.h

#ifndef CLASS
#define CLASS
class Trida {
private:
 int a; 
public:
 Trida(int n); 
 void fce();
};
#endif

class.cpp

#include <iostream>

using namespace std;

class Trida {
private:
 int a;

public:
 Trida(int n) {
  this->a = n;
 } 

 void fce() {
  cout << this->a << endl;
 }
};

Error message:

gwynbleidd@gwynbleidd-pc:~/Skola/test$ make
g++ class.cpp -c
g++ main.cpp -c
g++ class.o main.o -o test.exe
main.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Trida::Trida(int)'
main.cpp:(.text+0x54): undefined reference to `Trida::fce()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2024-10-18 14:11:58

所以这就是你做错的事情。在 class.cpp 中,您重新创建一个 Trida 类,而不是实现您在 class.h 中创建的类。你的 class.cpp 应该看起来更像这样:

#include <iostream>
#include "class.h"

using namespace std;

Trida::Trida(int n)
{
  this->a = n;
}

void Trida::fce() { cout << this->a << endl; }

并且实际上你应该在构造函数中使用初始化而不是赋值:

Trida::Trida(int n) : a(n) {}

So here's what you did wrong. In class.cpp you recreate a new Trida class rather than implement the one you've created in class.h. Your class.cpp should look more like this:

#include <iostream>
#include "class.h"

using namespace std;

Trida::Trida(int n)
{
  this->a = n;
}

void Trida::fce() { cout << this->a << endl; }

And really you should be using initialization rather than assignment in your constructor:

Trida::Trida(int n) : a(n) {}
酒绊 2024-10-18 14:11:58

您定义了类 trida 两次(在头文件 class.h 和源文件 class.cpp 中)
你的 class.cpp 文件应该是这样的

#include <iostream>
#include "class.h" //include "class.h"
using namespace std;

Trida::Trida(int n):a(n) //Initialization list
{
} 

void Trida::fce()
{
  cout << this->a << endl;
}

You are defining class trida two times (in the header file class.h and in the source file class.cpp)
Your class.cpp file should be like

#include <iostream>
#include "class.h" //include "class.h"
using namespace std;

Trida::Trida(int n):a(n) //Initialization list
{
} 

void Trida::fce()
{
  cout << this->a << endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文