C++ “未定义的引用......”

发布于 2024-12-10 08:31:59 字数 788 浏览 1 评论 0原文

我无法弄清楚为什么我的代码会被破坏,我需要一些帮助。

首先,代码:

Timer.h:

#include [...]

class Timer {
    public:
        [...]
        Timer operator+(double);
        [...]
    private:
        [...]
        void correctthedate(int day, int month, int year);
        [...]
};

Timer.cc:

#include "Timer.h"
using namespace std;

[...]

void correctthedate(int day, int month, int year) {
    [...]
}

[...]

Timer Timer::operator+(double plush) {
    [...]
    correctthedate(curday, curmonth, curyear);
    return *this;
}

当我尝试编译时,出现错误:

Timer.o: In function `Timer::operator+(double)':
Timer.cc:(.text+0x1ad3): undefined reference to `Timer::correctthedate(int, int, int)'

任何指针指向正确的方向吗?谢谢!

I cannot figure out why my code is breaking, and I could use some help.

First of all, the code:

Timer.h:

#include [...]

class Timer {
    public:
        [...]
        Timer operator+(double);
        [...]
    private:
        [...]
        void correctthedate(int day, int month, int year);
        [...]
};

Timer.cc:

#include "Timer.h"
using namespace std;

[...]

void correctthedate(int day, int month, int year) {
    [...]
}

[...]

Timer Timer::operator+(double plush) {
    [...]
    correctthedate(curday, curmonth, curyear);
    return *this;
}

When I try to compile I get the error:

Timer.o: In function `Timer::operator+(double)':
Timer.cc:(.text+0x1ad3): undefined reference to `Timer::correctthedate(int, int, int)'

Any pointers in the right direction? Thanks!

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

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

发布评论

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

评论(4

眼睛会笑 2024-12-17 08:31:59

以下行:

void correctthedate(int day, int month, int year) {

应该读取

void Timer::correctthedate(int day, int month, int year) {

否则,您只是定义了一个名为 Correctthedate() 的不相关函数。

The following line:

void correctthedate(int day, int month, int year) {

should read

void Timer::correctthedate(int day, int month, int year) {

Otherwise you're just defining an unrelated function called correctthedate().

牛↙奶布丁 2024-12-17 08:31:59

编写

void Timer::correctthedate(int day, int month, int year) {

您的 Correctthedate 定义是一个自由函数,尽管没有原型。您必须使用 Timer:: 限定名称

Write

void Timer::correctthedate(int day, int month, int year) {

Your correctthedate definition is a free function, albeit one without a prototype. You have to qualify the name with Timer::

以可爱出名 2024-12-17 08:31:59

将其替换

void correctthedate(int day, int month, int year) {

为:

Timer::correctthedate(int day, int month, int year) {

在您的版本中,Correctthedate 只是一个普通函数,它恰好与 Time 的方法之一具有相同的名称。 Time:: Correctthedate 是一个完全不同的函数(方法),它没有定义,因此链接器抱怨它找不到它。

Replace this:

void correctthedate(int day, int month, int year) {

With this:

Timer::correctthedate(int day, int month, int year) {

In your version, correctthedate is just an ordinary function, it just so happens that it has the same name as one of the methods of Time. Time::correctthedate is a completely different function (method) which doesn't have a definition, so the linker complains it can't find it.

北城孤痞 2024-12-17 08:31:59

您的标头声明了一个 Timer::operator+ 和一个 Timer:: Correctthedate 函数。
您的 cpp 定义了一个 Timer::operator+ 和一个 :: Correcttehdate 函数。
链接器找不到 Timer:: Correctthedate

答案是将 void Correctthedate(int... 更改为 void Timer:: Correctthedate(int...)。

Your header declares a Timer::operator+ and a Timer::correctthedate function.
Your cpp defines a Timer::operator+ and a ::correcttehdate function.
The linker can't find Timer::correctthedate.

The answer is to change void correctthedate(int... to void Timer::correctthedate(int....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文