0

我正在尝试在 C++ MSVS2010 中编译该程序,但遇到了奇怪的编译器错误,例如

 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)" (?returnRoll@Dice@@QAEHXZ) referenced in function "public: void __thiscall Dice::drawSpots(struct HDC__ *,int,int,int,int,int,int,int)" (?drawSpots@Dice@@QAEXPAUHDC__@@HHHHHHH@Z)    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;
}

**

编辑:是的,谢谢我收到链接器错误,但是将 Dice:: 添加到签名(似乎减少了编译器的抱怨但是)我仍然收到类似的不寻常错误,例如 -

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" (?result@@3PAHA) already defined in Dice.obj  C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \WinDemo \main.obj

Error   9   error LNK2005: "int count" (?count@@3HA) already defined in Dice.obj    C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \WinDemo \main.obj
4

4 回答 4

8

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

int Dice::returnRoll() {...}
    ^^^^^^
于 2012-07-31T13:21:19.387 回答
3

您忘记Dice::了 returnRoll 定义。

于 2012-07-31T13:21:55.290 回答
2

您忘记将 returnRoll 作为 Dice:: 的一部分

你只是错过了骰子::

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


int returnRoll()
{
    count=0;
    if((*result == 2)||(*result == 4)||(*result == 6))
    {
        count++;
    }
    return count;
}
于 2012-07-31T13:23:00.023 回答
1

根据你的新问题,我猜你没有提到你在两个文件中都包含了头文件,而不仅仅是主文件。你不应该在头文件中定义全局变量,否则你会遇到这样的问题。您需要将计数和结果的定义移动到源文件而不是标题。我没有看到您在主文件中使用它们,但如果您需要添加

extern int count;
extern int* result;

到头文件。

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

于 2012-08-01T13:57:04.723 回答