C++ “未定义的引用......”
我无法弄清楚为什么我的代码会被破坏,我需要一些帮助。
首先,代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下行:
应该读取
否则,您只是定义了一个名为
Correctthedate() 的不相关函数。
The following line:
should read
Otherwise you're just defining an unrelated function called
correctthedate()
.编写
您的
Correctthedate 定义是一个自由函数,尽管没有原型。您必须使用
Timer::
限定名称Write
Your
correctthedate
definition is a free function, albeit one without a prototype. You have to qualify the name withTimer::
将其替换
为:
在您的版本中,
Correctthedate 只是一个普通函数,它恰好与
Time 的方法之一具有相同的名称。
Time:: Correctthedate
是一个完全不同的函数(方法),它没有定义,因此链接器抱怨它找不到它。Replace this:
With this:
In your version,
correctthedate
is just an ordinary function, it just so happens that it has the same name as one of the methods ofTime
.Time::correctthedate
is a completely different function (method) which doesn't have a definition, so the linker complains it can't find it.您的标头声明了一个
Timer::operator+
和一个Timer:: Correctthedate
函数。您的 cpp 定义了一个
Timer::operator+
和一个:: Correcttehdate
函数。链接器找不到
Timer:: Correctthedate
。答案是将
void Correctthedate(int...
更改为void Timer:: Correctthedate(int...
)。Your header declares a
Timer::operator+
and aTimer::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...
tovoid Timer::correctthedate(int...
.