2012-06-09 30 views
1

首先發布在這裏,因爲我困在我的奇妙的C++函數中。函數中的鏈接器錯誤

我得到的錯誤是連接錯誤,並按如下:

main.obj:錯誤LNK2019:無法解析的外部符號「市民:無效thiscall控制:: printText(INT,INT,INT ,int,int,char const *,struct HWND *)「(?printText @ controls @@ QAEXHHHHHPBDPAUHWND __ @@@ Z)在函數中引用」long stdcall WndProc(struct HWND *,unsigned int,unsigned int,long) 「(?WndProc @@ YGJPAUHWND __ @@ IIJ @ Z)

C:\ Users \ HIDDEN \ Documents \ Visual Studio 2010 \ Projects \ TimedShutdown \ Debug \ TimedShut down.exe:致命>錯誤LNK1120:1周無法解析的外部

基本上我想有創建的Win32控件和繪畫文本和功能繪製文本就是出現我的問題的一類。

代碼如下:

的controls.h文件段: -

void printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent); 

的controls.cpp段

void printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent) 
{ 
    HDC hdc; 
    PAINTSTRUCT pss; 
    hdc = BeginPaint(parent, &pss); 
    SetBkMode(hdc, TRANSPARENT); 
    SetTextColor(hdc, RGB(R,G,B)); 
    TextOut(hdc, 30, 20, text, strlen(text));  
    EndPaint(parent, &pss); 
} 

的main.cpp中調用

controls ctrls; 
ctrls->printText(255,0,0,300,50,"Test text",hWnd); 

我已經刪除了通話,並仍然出現錯誤。最初我試圖將HDC和PAINTSTRUCT傳遞給該函數,但是我試圖識別錯誤源時刪除了它。

我完全失去了傢伙,即時通訊不是一個了不起的C++程序員,但我正在學習的過程。

批判我,我要求它!提前給任何幫助

謝謝,非常感謝:)

回答

3

你忘了告訴編譯器,在controls.cpp的功能是controls::printText。所以,編譯器仍然沒有定義。

修改你有controls.cpp做:

// This part is really important 
// It tells the compiler which function is defined 
//  | 
// vvvvvvvvvv 
void controls::printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent) 
{ // ... 

注意:傳遞給printText的顏色可能是R8G8B8,每個組件即8位。如果我是對的,你應該使用unsigned char而不是int代替R,GB

+0

這樣一個簡單的錯誤,我忽略= \非常感謝:D – Timmy

+0

不客氣;)請不要忘記接受anwser,通過點擊投票櫃檯底部的綠色勾號。 – Synxis

1

您還沒有指定controls::到你定義它的函數名。如果你不這樣做,你不能指望它像一個控件類的成員函數。 嘗試,代替目前的聲明本

void controls::printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent) 
{ 
    HDC hdc; 
    PAINTSTRUCT pss; 
    hdc = BeginPaint(parent, &pss); 
    SetBkMode(hdc, TRANSPARENT); 
    SetTextColor(hdc, RGB(R,G,B)); 
    TextOut(hdc, 30, 20, text, strlen(text));  
    EndPaint(parent, &pss); 
} 

編輯:它是不是從你在你的問題,你實際上已經得到了printText作爲控件的成員函數提供的代碼清晰,但以何種方式你從你的代碼中調用它意味着你打算如何運作。