2011-10-18 72 views
-1

我不明白为什么我的代码打破了,我可以使用一些帮助。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)' 

右侧目录中的任何指针挠度?谢谢!

+0

时间/计时器?你有一个混合在这里。 – crashmstr

回答

7

下面一行来限定名称:

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

应该读

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

否则你只是定义称为correctthedate()无关的功能。

+0

太棒了!非常感谢! – adammenges

+1

任何曾经犯过这个错误的人,举手:!_! –

5

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

correctthedate定义是一个免费的功能,虽然它没有一个原型。你有Timer::

2

替换此:

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

有了这个:

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

在你的版本,correctthedate只是一个普通的功能,它只是恰巧,它具有相同的名称的方法之一的TimeTime::correctthedate是一个完全不同的函数(方法),它没有定义,所以链接器抱怨找不到它。

1

您的标题声明了Timer::operator+Timer::correctthedate函数。
您的CPP定义了Timer::operator+::correcttehdate函数。
链接程序找不到Timer::correctthedate

答案是将void correctthedate(int...更改为void Timer::correctthedate(int...