2012-09-08 93 views
-5

爲什麼會發生這種情況?Win32靜態變量鏈接錯誤

1> ------構建開始:項目:客戶端,配置:調試的Win32 ------
1> WindowManager.cpp
1>生成代碼...
1 >編譯...
1> Main.cpp的
1>生成代碼...
1> WindowManager.obj:錯誤LNK2001:解析外部符號
? 「私人:靜態INT窗口管理器:: win_stat」(win_stat @ WindowManager @@ 0HA) 1> C:\ Users \ user \ documents \ visual studio 2012 \ Projects \ TalkToMe \ Debug \ Client.exe:致命錯誤LNK1120:1無法解析的外部設備
==========構建:0成功,1失敗,0最新,0跳過= =========

WindowManager.cpp

#include "WindowManager.h" 

void WindowManager::PekMessage() 
{ 
    if(PeekMessage(&msg, window, 0, 0, PM_REMOVE)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
} 

bool WindowManager::isWindowOpen() 
{ 
    return win_stat != -1; 
} 

HWND WindowManager::textbox(int width, int height, int xPos, int yPos, LPCSTR content, bool edit_able) 
{ 
    int type = (edit_able) ? (WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL) : (WS_CHILD|WS_VISIBLE|WS_HSCROLL|ES_AUTOHSCROLL); 
    return CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     "EDIT", 
     content, 
     type, 
     xPos, 
     yPos, 
     width, 
     height, 
     window, 
     (HMENU)50, 
     GetModuleHandle(NULL), 
     NULL 
    ); 
} 

HWND WindowManager::textbox(Vector size, Vector position, LPCSTR content, bool edit_able) 
{ 
    return textbox(size.getX(), size.getY(), position.getX(), position.getY(), content, edit_able); 
} 

LRESULT CALLBACK WindowManager::WindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2) 
{ 
    switch(uint_Message) 
    { 
     case 16: // exit button 
      win_stat = -1; 
     break; 
    } 

    return DefWindowProc(winhan,uint_Message,parameter1,parameter2); 
} 

WindowManager::WindowManager(LPCTSTR title,int x, int y, int width, int height) 
{ 
    WNDCLASSEX wnd; 
    wnd.cbSize = sizeof(wnd); 
    wnd.style = CS_HREDRAW | CS_VREDRAW; 
    wnd.lpfnWndProc = WindowProcedure; 
    wnd.cbClsExtra = 0; 
    wnd.cbWndExtra = 0; 
    wnd.hInstance = GetModuleHandle(NULL); 
    wnd.hIcon  = NULL; 
    wnd.hCursor  = LoadCursor(NULL,IDC_ARROW); 
    wnd.hbrBackground = GetSysColorBrush(NULL); 
    wnd.lpszClassName = "TalkToMe"; 
    wnd.lpszMenuName = NULL; 
    wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    RegisterClassEx(&wnd); 

    window = CreateWindowEx(WS_EX_CONTROLPARENT, wnd.lpszClassName, title, 
     WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, x, y, width, height,NULL, NULL, 
     GetModuleHandle(NULL), NULL); 
} 

WindowManager::~WindowManager() 
{ 
    DestroyWindow(window); 
} 

WindowManager.h

#pragma once 
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") 

#include <Windows.h> 
#include "Vector.h" 

class WindowManager 
{ 
private: 
    MSG msg; 
    HWND window; 

    static int win_stat; 
public: 
    WindowManager(LPCTSTR title,int x, int y, int width, int height); 
    ~WindowManager(); 
    static LRESULT CALLBACK WindowProcedure(HWND winhan,UINT uint_Message,WPARAM parameter1,LPARAM parameter2); 

    HWND textbox(int width, int height, int xPos, int yPos, LPCSTR content, bool edit_able=true); 
    HWND textbox(Vector size, Vector position, LPCSTR content, bool edit_able=true); 

    bool isWindowOpen(); 
    void PekMessage(); 
}; 
+0

重複:http://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members – tenfour

回答

3

在標題中的部分

static int win_stat; 

只是一個聲明。您也可以

int WindowManager::win_stat; 

添加到.cpp文件,就像你定義的成員函數存在。

+0

謝謝你們,但它再次顯示我錯誤:/ this error「1> WindowManager.obj:錯誤LNK2001:無法解析的外部符號「private:static int WindowManager :: win_stat」(?win_stat @ WindowManager @@ 0HA)「 –

+0

然後,您要麼得到該行錯誤,要麼沒有鏈接到具有該行的編譯版本的文件在裏面。 –

+0

那我該如何解決它? –