C++程序中不寻常的编译器错误.没有语法相关.Windows编程

sta*_*ser 0 c++ visual-c++

我正在尝试在C++ MSVS2010中编译这个程序,并得到奇怪的编译器错误,如

 Error  12  error LNK1120: 1 unresolved externals   C:\Users\win7vm\Documents\Visual Studio 2010\Projects\WinDemo \Debug\WinDemo.exe    1.
Run Code Online (Sandbox Code Playgroud)

我也得到了

 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
Run Code Online (Sandbox Code Playgroud)

这两者似乎都与语法无关,我并没有完全理解它们.基本上,我们的想法是让switch语句显示从另一个函数传递给该函数的值.我几乎可以肯定这是一个简单的参考/指针问题,我完全不知道了.这个类有一个标题,一个windows.h(开箱即用的未修改,所以你可以在需要的时候查找它),以及带有主文件的函数(这里包含的函数和类).让我预先回答几乎肯定会出现的两个问题: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;
}
Run Code Online (Sandbox Code Playgroud)

**

编辑:是的,谢谢我收到链接器错误,但添加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
Run Code Online (Sandbox Code Playgroud)

chr*_*ris 8

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

int Dice::returnRoll() {...}
    ^^^^^^
Run Code Online (Sandbox Code Playgroud)