2012-07-31 33 views
0

我想编译这个程序在C++ MSVS2010和我得到奇编译器错误,如C++程序中不寻常的编译器错误。没有语法相关。 Windows编程

Error 12 error LNK1120: 1 unresolved externals C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \Debug\WinDemo.exe 1. 

,我也越来越

Error 11 error LNK2019: unresolved external symbol "public: int __thiscall Dice::returnRoll(void)" ([email protected]@@QAEHXZ) referenced in function "public: void __thiscall Dice::drawSpots(struct HDC__ *,int,int,int,int,int,int,int)" ([email protected]@@[email protected]@[email protected]) C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \WinDemo \Debug\WinDemo.exe 1\Dice.obj 

均未似乎是语法相关,我不完全理解他们。 基本上,这个想法是让switch语句显示从另一个函数传入该函数的值。我几乎可以肯定,这是一个简单的参考/指针问题,我完全错过了。这个类有一个头文件,一个windows.h(开箱即用,不需要修改,所以如果需要的话可以查看它),以及带有主文件(这里包含的函数和类)的函数。让我预先回答2个几乎肯定会出现的问题:a)是的,它必须使用C++中的windows.h文件。 b)是的,我知道有一百个更好的图书馆和语言可以用来解决这个问题,我在这里没有选择。有人也可能会试图指出所声明的全局变量。在解决相对较短的代码块时,全局变量是暂时的但是必要的不便。我意识到你们中许多人对C++非常熟练,但我要求你耐心地用我的低级C++编程来解释你的答案。谢谢。 :)

** //头文件

#include <windows.h> 
#include <math.h> 
#include <vector> 
using namespace std; 

int count=0; 
int *result = &count; 

class Dice 
{ 
public: 
    void drawSpots(HDC hdc, int x1,int y1, int x2,int y2, int r, int g,int b); 
    int roll(); 
    wchar_t * drawString(); 
    int returnRoll(); 
private: 
    int x1,x2,y1,y2,sr,sg,sb; 
    //int &count; 
    //int *result; 
    int value; 
    int spotColor; 
}; 


//definitions file 


void Dice::drawSpots(HDC hdc, int x1,int y1, int x2,int y2, int r, int g,int b) 
{ 
    *result = roll(); 
    //vectorOfDice.push_back(result); 
    //wchar_t * drawString(); 
    returnRoll(); 
    HBRUSH hBrush = CreateSolidBrush(RGB(r,g,b)); 
    SelectObject(hdc,hBrush); 
    switch(*result) 
    { 
    case 1: 
     //draw something based on case 
     break; 
    case 2: 
     //draw something based on case 
     break; 
    case 3: 
     //draw something based on case 
     break; 
    case 4: 
     //draw something based on case 
     break; 
    case 5: 
     //draw something based on case  
        break; 
    case 6: 
     //draw something based on case  
        break; 
    } 

    DeleteObject(hBrush); 
} 

int Dice::roll() 
{ 
    value=1+(rand()%6); 
    return value; 
} 


int Dice::returnRoll() 
{ 
    count=0; 
    if((*result == 2)||(*result == 4)||(*result == 6)) 
    { 
     count++; 
    } 
    return count; 
} 

wchar_t * Dice::drawString() 
{ 
    //result = returnRoll(result); 
    //result = roll(); 
    switch(*result) 
    { 
    case 0: 
     return L"no"; 
     break; 
    case 1: 
     return L"1"; 
     break; 
    case 2: 
     return L"2"; 
     break; 
    case 3: 
     return L"3"; 
     break; 
    case 4: 
     return L"4"; 
     break; 
    case 5: 
     return L"5"; 
     break; 
    case 6: 
     return L"6"; 
     break; 
    } 
    return L"X"; 
} 


//main file 


#include <windows.h> 
#include <math.h> 
#include "Dice.h" 


const wchar_t g_szClassName[] = L"myWindowClass"; 



// Step 4: the Window Procedure 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    Dice dice; 

    //state variables 
    static int red = 0; 
    static int green = 0; 
    static int blue = 100; 

    static int tr = 0; 
    static int tg = 0; 
    static int tb = 0; 

    HDC hDC; 
    PAINTSTRUCT Ps; 
    HPEN hPen1; 
    HPEN hPen2; 


    HBRUSH hBrush1; 
    HFONT hFont1; 
    RECT rect; 
    int fontHeight = 100; 


    switch(msg) 
    { 
     case WM_PAINT: 
      hDC = BeginPaint(hwnd,&Ps); 

      hPen1=CreatePen(PS_SOLID,5,RGB(red,green,blue)); 
      hPen2=CreatePen(PS_SOLID,5,RGB(0,0,0)); 

      hBrush1= CreateSolidBrush(RGB(0,0,255)); 

      SelectObject(hDC,hPen1); 
      SelectObject(hDC,hBrush1); 

      fontHeight=50; 
      hFont1=CreateFont(fontHeight,0,0,0,0,1,0,0,0,0,0,0,0,L"Times New Roman"); 
      //hFont1=CreateFont(fontHeight,0,0,0,0,0,0,0,0,0,0,0,0,L"Arial"); 
      SelectObject(hDC,hFont1); 

      rect.top=100; 
      rect.bottom=700; 
      rect.left = 550; 
      rect.right =950; 

      DrawText(hDC,L"There are ",-1,&rect,DT_CENTER | DT_WORDBREAK); 
      rect.top=200; 
      DrawText(hDC,dice.drawString(),-1,&rect,DT_CENTER | DT_WORDBREAK); 
      rect.top=300; 
      DrawText(hDC,L" even dice!",-1,&rect,DT_CENTER | DT_WORDBREAK); 


      DeleteObject(hBrush1); 
      DeleteObject(hPen2); 
      DeleteObject(hPen1); 
      EndPaint(hwnd,&Ps); 
     break; 
     case WM_KEYDOWN: 
      if(wParam==VK_SPACE) 
      { 
       dice.roll(); 
       InvalidateRect(hwnd,NULL,true); 
      } 
      break; 
     case WM_CHAR: 
      if(wParam=='c') 
      { 
       tr=rand()%256; 
       tg=rand()%256; 
       tb=rand()%256; 
       dice.spotColors(); 
      } 
      InvalidateRect(hwnd,NULL,true); 
      break; 
     case WM_CLOSE: 
      DestroyWindow(hwnd); 
     break; 
     case WM_DESTROY: 
      PostQuitMessage(0); 
     break; 
     default: 
      return DefWindowProc(hwnd, msg, wParam, lParam); 
    } 
    return 0; 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow) 
{ 
    WNDCLASSEX wc; 
    HWND hwnd; 
    MSG Msg; 

    //Step 1: Registering the Window Class 
    wc.cbSize  = sizeof(WNDCLASSEX); 
    wc.style   = 0; 
    wc.lpfnWndProc = WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance  = hInstance; 
    wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = g_szClassName; 
    wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 

    if(!RegisterClassEx(&wc)) 
    { 
     MessageBox(NULL, L"Window Registration Failed!", L"Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    // Step 2: Creating the Window 
    hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE, 
     g_szClassName, 
     L"window", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, 1000, 800, 
     NULL, NULL, hInstance, NULL); 

    if(hwnd == NULL) 
    { 
     MessageBox(NULL, L"Window Creation Failed!", L"Error!", 
      MB_ICONEXCLAMATION | MB_OK); 
     return 0; 
    } 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 

    // Step 3: The Message Loop 
    while(GetMessage(&Msg, NULL, 0, 0) > 0) 
    { 
     TranslateMessage(&Msg); 
     DispatchMessage(&Msg); 
    } 
    return Msg.wParam; 
} 

**

编辑: 是感谢我得到了链接错误,但加入骰子::签字(似乎降低编译器抱怨但)我依然得到类似的不寻常的错误,比如 -

Error 11 error LNK1169: one or more multiply defined symbols found C:\Users\win7vm\Documents\Visual Studio 2010\Projects\\WinDemo \Debug\WinDemo.exe 1 

Error 10 error LNK2005: "int * result" ([email protected]@3PAHA) already defined in Dice.obj C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \WinDemo \main.obj 

Error 9 error LNK2005: "int count" ([email protected]@3HA) already defined in Dice.obj C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \WinDemo \main.obj 
+2

常见的人为什么downvotes。仅仅因为你认为答案微不足道并不意味着它是针对新人的。你应该鼓励初学者不要因此而感到不快。 – 2012-07-31 15:01:51

+0

其实我自己解决了。我在我的函数文件中再次定义了计数变量(不是主要的)并编译了它。从来没有碰到过,似乎它与windows.h有关,作为头文件之一(来自我自己的研究,也来自于一位程序员的建议,他说自己已经与之合作)以及变量的方式在不同的文件中定义但未正确链接。 – stackuser 2012-10-27 15:22:58

回答

8

你没有把Dice::在01的签名的定义。这会导致链接器在需要时永远不会找到它,因此会给你一个链接器错误(不是编译器错误)。

int Dice::returnRoll() {...} 
    ^^^^^^ 
3

您忘记了Dice:: in returnRoll定义。

2

你忘了做骰子::

根据您的新的问题,你只是缺少骰子::

int Dice::roll() 
{ 
    value=1+(rand()%6); 
    return value; 
} 


int returnRoll() 
{ 
    count=0; 
    if((*result == 2)||(*result == 4)||(*result == 6)) 
    { 
     count++; 
    } 
    return count; 
} 
1

returnRoll部分,我猜你没有提到您将头文件包含在两个文件中,而不仅仅是主文件中。你不应该在头文件中定义全局变量,或者你得到这样的问题。您需要将计数和结果的定义移到源文件而不是头部。我没有看到你在主文件中使用它们,但是如果你需要将

extern int count; 
extern int* result; 

添加到头文件中。

更好的办法是避免使用全局变量,这不是你需要它们的情况。而不是让drawSpots设置一个全局变量,为什么不只是返回值呢?