Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I get the following error(s) on my program that captures the mouse and then draws a line. Errors:

1>------ Build started: Project: Capture_Mouse_Line, Configuration: Debug Win32 ------
1>  main.cpp
1>main.obj : error LNK2001: unresolved external symbol "public: static long * Line::yc2" (?yc2@Line@@2PAJA)
1>main.obj : error LNK2001: unresolved external symbol "public: static long * Line::xc2" (?xc2@Line@@2PAJA)
1>main.obj : error LNK2001: unresolved external symbol "public: static long * Line::yc1" (?yc1@Line@@2PAJA)
1>main.obj : error LNK2001: unresolved external symbol "public: static long * Line::xc1" (?xc1@Line@@2PAJA)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>D:\Visual C++ Projects\Capture_Mouse_Line\Debug\Capture_Mouse_Line.exe : fatal error LNK1120: 5 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Here is my code:

#include<allegro5\allegro.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_primitives.h>
#include<Windows.h>
#include<allegro5\allegro_windows.h>

#ifndef WIDTH
#define WIDTH 1440
#endif

#ifndef HEIGHT 
#define HEIGHT 900
#endif

class Line
{
public:
    static void ErasePreviousLine();

    static void DrawLine(long* x, long* y,long* x2,long* y2);

    static bool Erasable();


static long* xc1;
static long* yc1;
static long* xc2;
static long* yc2;
};

 void Line::ErasePreviousLine()
{

    delete xc1;
    xc1 = NULL;
    delete yc1;
    yc1 = NULL;
    delete xc2;
    xc2 = NULL;
    delete yc2;
    yc2 = NULL;
 }

 bool Line::Erasable()
 {
     if(xc1 && yc1 && xc2 && yc2 == NULL)
     {
         return false;
     }
     else
     {
         return true;
     }
 }

 void Line::DrawLine(long* x,long* y,long* x2,long* y2)
 {

     if(!al_init_primitives_addon())
     {
         al_show_native_message_box(NULL,NULL,NULL,"failed to initialize allegro",
        NULL,NULL);
     }

     xc1 = x;
     yc1 = y;
     xc2 = x2;
     yc2 = y2;

     al_draw_line((float)*xc1, (float)*yc1, (float)*xc2, (float)*yc2,al_map_rgb(255,0,255), 1);

     delete x;
     delete y;
     delete x2;
     delete y2;
 }


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;


    ALLEGRO_DISPLAY* display = NULL;

    if(!al_init())
    {
        al_show_native_message_box(NULL,NULL,NULL,"failed to initialize allegro",
        NULL,NULL);

        return -1;
    }

    display = al_create_display(WIDTH,HEIGHT);

    if(!display)
    {
        al_show_native_message_box(NULL,NULL,NULL,"failed to initialize display",
        NULL,NULL);

        return -1;
    }

    HWND hwnd = al_get_win_window_handle(display);


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


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


    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static bool bIsCaptured;
    static POINTS ptsBegin;
    static POINTS ptsEnd;
    switch(msg)
    {
    case WM_LBUTTONDOWN:
        SetCapture(hwnd);
        bIsCaptured = true;
        ptsBegin = MAKEPOINTS(lParam);
        return 0;
    case WM_MOUSEMOVE:
        if(wParam & MK_LBUTTON)
        {
            if(!Line::Erasable())
            {
                return 0;
            }

            Line::ErasePreviousLine();

            ptsEnd = MAKEPOINTS(lParam);

            Line::DrawLine(new long(ptsBegin.x),new long(ptsBegin.y),new long(ptsEnd.x),new long(ptsEnd.y));
        }
        break;

    case WM_LBUTTONUP:
        bIsCaptured = false;
        ReleaseCapture();
        break;

    case WM_ACTIVATEAPP:
        {
            if(wParam == TRUE)
            {
                if(bIsCaptured){
                    SetCapture(hwnd);}
            }
        }
        break;
    }
    return 0;
}
share|improve this question
add comment

1 Answer

You need to define the static variables explicit:

static long* xc1;
static long* yc1;
static long* xc2;
static long* yc2;

In the implementation file (.cpp), try adding this code:

long* Line::xc1 = NULL;
long* Line::yc1 = NULL;
long* Line::xc2 = NULL;
long* Line::yc2 = NULL;

More info at: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

edit: I think the definition of the static members MUST be in the implementation file.

share|improve this answer
    
Good, then I get this error message: 1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup –  random Oct 29 '12 at 15:43
    
@random a wild guess would suggest something with your "main" function, but I dont code for Windows. Any reason not to manage your application window with just Allegro? (without #include <windows.h>)? Like in this link –  AranHase Oct 29 '12 at 16:24
    
you need, in the options to set your subsystem –  CobaltHex Oct 29 '12 at 16:39
    
In C++03, a static member must be defined in exactly one translation unit. You can omit the definition if the variable is static const integral and you don't ever take the address of it. –  Lars Viklund Oct 29 '12 at 18:01
    
What do I need to setup in the subsystem? –  random Oct 29 '12 at 18:06
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.