2011-10-18 186 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...