文件包含错误 (C++):未定义对 ______ 的引用

发布于 2024-12-24 22:16:14 字数 1180 浏览 1 评论 0原文

每当我编译 #includes 用户定义类的内容时,我都会收到这些编译错误,这些错误总是看起来像: main.cpp: undefined reference to Complex::Complex(double, double)

我已经减少了将问题归结为一组三个极其简单的文件:main.cpp,以及例如Complex.h 和Complex.cpp。我仍然收到未定义的参考错误。我在 Windows 上使用 Code::Blocks 进行开发,但在 Ubuntu 中使用 g++ 也得到了同样的结果。为什么会出现这种情况?我尝试过在 Code::Blocks 中的 main.cpp 之前构建 Complex.cpp,并且尝试过 g++ main.cpp Complex.cpp 就像我尝试过 g++ main 一样.cpp。每次都出现同样的错误。

/*======== main.cpp ========*/
#include "Complex.h"

int main()
{
    Complex A(1.0, 1.0);
    return 0;
}

/*======== Complex.h ========*/
#ifndef _COMPLEX_H
#define _COMPLEX_H

class Complex
{
    public:
    double x, y;
    Complex(double real, double imag);
};

#endif

/*======== Complex.cpp ========*/
#include "Complex.h"

Complex::Complex(double real, double imag)
{
    x = real;
    y = imag;
}

ed:现在我得到了不同的错误,所以我一定做错了一些事情。使用与上面相同的代码,我得到:

main.cpp: in function 'int main()':
main.cpp:5:5: error: 'Complex' was not declared in this scope
main.cpp:5:13: error: expected ';' before 'A'

这很奇怪。当我将类放在 .cpp 文件中时,一切都可以正常工作,但这是“不好的做法”,因此我将类定义移到 .h 文件中,并将实现保留在 .cpp 文件中,但现在没有任何效果。

Whenever I compile something that #includes a user-defined class, I get these compilation errors that always look like: main.cpp: undefined reference to Complex::Complex(double, double)

I've reduced the problem to a set of three extremely bare files: main.cpp, and for example, Complex.h and Complex.cpp. I still get undefined reference errors. I'm developing in Code::Blocks on Windows but I get the same thing using g++ in Ubuntu. Why does this happen? I've tried building Complex.cpp before main.cpp in Code::Blocks, and I've tried g++ main.cpp Complex.cpp as much as I've tried just g++ main.cpp. Same errors every time.

/*======== main.cpp ========*/
#include "Complex.h"

int main()
{
    Complex A(1.0, 1.0);
    return 0;
}

/*======== Complex.h ========*/
#ifndef _COMPLEX_H
#define _COMPLEX_H

class Complex
{
    public:
    double x, y;
    Complex(double real, double imag);
};

#endif

/*======== Complex.cpp ========*/
#include "Complex.h"

Complex::Complex(double real, double imag)
{
    x = real;
    y = imag;
}

ed: now I get different errors so I must be doing something completely wrong. Using the same code as above, I get:

main.cpp: in function 'int main()':
main.cpp:5:5: error: 'Complex' was not declared in this scope
main.cpp:5:13: error: expected ';' before 'A'

This is bizarre. Everything worked earlier when I had the class in a .cpp file, but that's "bad practice" so I moved my class definitions into .h files and kept the implementation in .cpp files, and now nothing works.

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

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

发布评论

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

评论(2

删除会话 2024-12-31 22:16:14

这不是编译错误,而是链接错误。您需要确保将所有对象链接在一起。您可以通过几种方式做到这一点:

g++ main.cpp Complex.cpp

应该可以正常工作(当我尝试使用您的示例时,这里确实如此)。您也可以分步骤进行:

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

That's not a compilation error, it's a link error. You need to make sure to link all of your objects together. You can do that in a couple ways:

g++ main.cpp Complex.cpp

Should work fine (and does here when I tried with your example). You can also do it in steps:

g++ -c main.cpp
g++ -c Complex.cpp
g++ main.o Complex.o
若沐 2024-12-31 22:16:14

虽然我们不知道这是否是实际的代码(可能不适用于其他几个代码),但让我们稍微评论一下代码本身......(这不会对链接器错误产生任何影响

  1. )下划线后跟大写字母,例如_COMPLEX_H,保留用于C++编译器和C++标准库的实现。 不要使用它们。
  2. 成员变量最好设为私有。很少需要将实际数据成员设为 public(有时将非函数成员设为 public 是合理的,例如用户可以订阅回调的事件类,但这些尽管它们在技术上是对象,但通常表现得像函数)。
  3. 初始化最好在成员初始值设定项列表中完成。也就是说,构造函数看起来像这样:

    复杂::复杂(双实数,双图像):
    x(实数),
    y(图像)
    {
    }

,大胆猜测一下实际代码出了什么问题导致了链接问题:

  1. 构造函数被定义为内联。显然,除非定义在使用构造函数的地方可见,否则这将不起作用。
  2. Complex 的声明以某种方式将其放入未命名的命名空间中,因此该定义恰好定义了一个与 main.cpp 中看到的类不同的类。

While we are left in the dark whether this is the actual code (probably not as it works for several others), let's comment on the code itself a bit... (this won't have any effect on the linker error)

  1. Names starting with an underscore followed by a capital letter, e.g. _COMPLEX_H, are reserved for the implementation of the C++ compiler and the C++ standard library. Don't use them.
  2. Member variables are best made private. There is rarely any need to make actual data member public (sometimes it is reasonable to make non-function members public, e.g. an event class where users can subscribe callbacks, but these typically behave like functions although they are technically objects).
  3. Initialization is best done in the member initializer list. That is, the constructor would look something like this:

    Complex::Complex(double real, double image):
    x(real),
    y(imag)
    {
    }

Finally, to venture a few guesses what is going wrong with the actual code to cause the linking problem:

  1. The constructor is defined to be inline. Obviously, this won't work unless the definition is visible where the constructor is used.
  2. The declaration of Complex somehow made it into an unnamed namespace and thus the definition happens to define a different class than the one seen by main.cpp.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文