[英]C++ “undefined reference to…”
我無法弄清楚為什么我的代碼會中斷,我可以使用一些幫助。
首先,代碼:
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)'
任何指向正確方向的指針? 謝謝!
下一行:
void correctthedate(int day, int month, int year) {
應該讀
void Timer::correctthedate(int day, int month, int year) {
否則,您僅定義了一個不相關的函數,稱為correctthedate()
。
寫
void Timer::correctthedate(int day, int month, int year) {
你correctthedate
定義是一個免費的功能,雖然它沒有一個原型。 您必須使用Timer::
限定名稱Timer::
替換為:
void correctthedate(int day, int month, int year) {
有了這個:
Timer::correctthedate(int day, int month, int year) {
在您的版本中, correctthedate
只是一個普通函數,它恰巧與它的Time
方法之一具有相同的名稱。 Time::correctthedate
是一個完全不同的函數(方法),沒有定義,因此鏈接器抱怨找不到它。
您的標頭聲明了一個Timer::operator+
和一個Timer::correctthedate
函數。
您的cpp定義了Timer::operator+
和::correcttehdate
函數。
鏈接器找不到Timer::correctthedate
。
答案是將void correctthedate(int...
更改為void Timer::correctthedate(int...
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.