Using Classes and Structs

I am confused on using Classes and Structs.
Can anyone shed light on what MS has changed about them

example this code generates a ton of weird errors



#include <windows.h>
class CIGECORE
{
public:
    LPSTR sAppTitle;
    int iPosX;
    int iPosY;
    int iWidth;
    int iHeight;
};

 



Answer this question

Using Classes and Structs

  • DaveJohnson

    ok I did that, and I tried using a pragma to include the user32lib but still getting link errors:

    Windows.cpp



    #pragma lib("user32.lib")

    #define WIN32_LEAN_AND_MEAN // DO NOT USE MFC

    #include <windows.h>
    #include <windowsx.h>
    #include "IGE.h"
    #include "main.h"

    #define WINDOWSCLASS "WINCLASS1" // DECLARE WINCLASS CONSTANT

    CIGECORE            IGECORE;

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    // EVENT HANDELER/////////////////////////////////////////////////////////////
    {
        PAINTSTRUCT ps;
        HDC            hdc;

        switch(msg)
        {
        case WM_CREATE:
            {
                // WINDOW CREATION CODE
                return(0);
            }break;
        case WM_PAINT:
             {
                 hdc = BeginPaint(hwnd,&ps);
                 EndPaint(hwnd,&ps);
                 return(0);
             }break;
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return(0);
            }break;
        default:break;
        }
        return(DefWindowProc(hwnd,msg,wparam,lparam));
    }

    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
                       LPSTR lpcmdline, int ncmdshow)
    // WIN MAIN /////////////////////////////////////////////////////////////
    {
        WNDCLASSEX winclass;
        HWND hwnd;
        MSG msg;

        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_HREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hinstance;
        winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = WINDOWSCLASS;
        winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

        if(!RegisterClassEx(&winclass)){
            return(0);
        }

        if(!(hwnd = CreateWindowEx(NULL,WINDOWSCLASS,IGECORE.sAppTitle,WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            IGECORE.iPosX,IGECORE.iPosY,IGECORE.iWidth,IGECORE.iHeight,NULL,NULL,hinstance,NULL)))
        {
            return(0);
        }

        while(TRUE)
        {
            if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
            {
                if(msg.message == WM_QUIT)
                {
                    break;
                }
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            IGERUNTIME();
        }
    }

     


    ------ Build started: Project: Insane Game Engine, Configuration: Debug Win32 ------
    Compiling...
    Windows.cpp
    .\Windows.cpp(7) : warning C4068: unknown pragma
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\windows.cpp(93) : warning C4715: 'WinMain' : not all control paths return a value
    Linking...
    Windows.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__EndPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcA@16 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageA@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__PeekMessageA@20 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__RegisterClassExA@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__LoadCursorA@8 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__LoadIconA@8 referenced in function _WinMain@16
    C:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Debug\Insane Game Engine.exe : fatal error LNK1120: 12 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm"
    Insane Game Engine - 13 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


  • Irfan Habib

    It has WinMain for an entry point.

    windows.cpp



    #define WIN32_LEAN_AND_MEAN // DO NOT USE MFC

    #include <windows.h> // WINDOWS HEADER FILES
    #include <windowsx.h> // EXTRA DEFINES MACROS ETC
    #include "IGE.h"
    #include "IGE2D.h"
    #include "IGE3D.h"
    #include "IGENETWORK.h"
    #include "IGEPHYSICS.h"
    #include "main.cpp"

    #define WINDOWSCLASS "WINCLASS1" // DECLARE WINCLASS CONSTANT

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    // EVENT HANDELER/////////////////////////////////////////////////////////////
    {
        PAINTSTRUCT ps;
        HDC            hdc;

        switch(msg)
        {
        case: WM_CREATE:
            {
                // WINDOW CREATION CODE
                CIGECORE            IGECORE;
                CIGE2D                IGE2D;
                CIGE3D                IGE3D;
                CIGENETWORK            IGENETWORK;
                CIGEPHYSICS            IGEPHYSICS;
                return(0);
            }break;
        case:WM_PAINT
             {
                 hdc = BeginPaint(hwnd,&ps);
                 EndPaint(hwnd,&ps);
                 return(0);
             }break;
            case WMDESTROY
            {
                PostQuitMessage(0);
                return(0);
            }break;
        default:break;
        }
        return(DefWindowProc(hwnd,msg.wparam,lparam));
    }

    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
                       LPSTR lpcmdline, int ncmdshow)
    // WIN MAIN /////////////////////////////////////////////////////////////
    {
        WNDCLASSEX winclass;
        HWND hwnd;
        MSG msg;

        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_HREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hinstance;
        winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = WINDOWSCLASS;
        winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

        if(!RegisterClassEx(&winclass)){
            return(0);
        }

        if(!(hwnd = CreateWindowEx(NULL,WINDOWSCLASS,IGECORE.sAppTitle,WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            IGECORE.iPosX,IGECORE.iPosY,IGECORE.iWidth,IGECORE.iHeight,NULL,NULL,hinstance,NULL)))
        {
            return(0);
        }

        while(TRUE)
        {
            if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
            {
                if(msg.message == WM_QUIT)
                {
                    break;
                }
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            IGERUNTIME();
        }
    }

     


  • spuckupine

    A struct is just a class whose members default to public access ( for a class, it's private ).  I doubt very much that Microsoft have changed anything to do with classes and structs.

    A .NET class and a .NET struct is a very different beast, and if you're using C++/CLI, you may have to do some reading there, but in straight C++, the difference is trivial.



  • DataBound

    eh I see Big Smile

    Now only these 2.

    Compiling...
    Windows.cpp
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\windows.cpp(91) : warning C4715: 'WinMain' : not all control paths return a value
    Linking...
    Windows.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
    C:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Debug\Insane Game Engine.exe : fatal error LNK1120: 1 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm"
    Insane Game Engine - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • rishi bajaj

    Heh well thats quite embarassing.
    Anyway, made those changes.

    Now I got 12 linker errors :x

    ------ Build started: Project: Insane Game Engine, Configuration: Debug Win32 ------
    Compiling...
    Windows.cpp
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\windows.cpp(91) : warning C4715: 'WinMain' : not all control paths return a value
    Linking...
    Windows.obj : error LNK2005: "void __cdecl IGERUNTIME(void)" ( IGERUNTIME@@YAXXZ) already defined in main.obj
    Windows.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__EndPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcA@16 referenced in function "long __stdcall WindowProc(struct HWND__ *,unsigned int,unsigned int,long)" ( WindowProc@@YGJPAUHWND__@@IIJ@Z)
    Windows.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageA@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__PeekMessageA@20 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__RegisterClassExA@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__LoadCursorA@8 referenced in function _WinMain@16
    Windows.obj : error LNK2019: unresolved external symbol __imp__LoadIconA@8 referenced in function _WinMain@16
    C:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Debug\Insane Game Engine.exe : fatal error LNK1120: 12 unresolved externals
    Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm"
    Insane Game Engine - 14 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • RichardAB

    OK, in that case, at least one of your errors is caused by your class being included more than once, you don't have an inclusion guard in there ( the #ifndef blah stuff at the top of most Windows classes ).  Also, I notice you're including windows.h in your other class, as well as here - why Are you not using precompiled headers

    Why does windows.cpp #include main.cpp


  • kadonk

    Then the pragma doesn't work.  I never do it that way, I add the library to the link settings of the project file.


  • Andre Da Costa

    I tried the sample you provided and it compiled with no errors. What kind of errors you are seeing

    Thanks,
      Ayman Shoukry
      VC++ Team



  • Leijtens

    This code is riddled with errors

    case: WM_CREATE

    For a case statement, the : comes at the end, not the middle

    case WMDESTROY

    it's WM_DESTROY

    IGECORE.iPosX,IGECORE.iPosY,IGECORE.iWidth,IGECORE.iHeight

    case WM_CREATE:

    {

    // WINDOW CREATION CODE

    CIGECORE IGECORE;

    return(0);

    }break;

    The {} define a scope.  Your CIGECORE instance exists for but a moment.  I also don't see anywhere that you initialise it's values.  It certainly does not exist when you're trying to use it.



    return(DefWindowProc(hwnd,msg.wparam,lparam));

    msg.wparam should be msg, wparam.

    In other words, Microsoft have made no changes to how structs and classes work, but your code is completely riddled with errors.


  • flape

    Your project needs to link against User32.lib, at least.  Then add a file called main.h., which looks like this:

    void IGERUNTIME();

    and add #include "main.h" at the top of main.cpp.  Then change your #include in windows.cpp to be main.h.



  • Joe McCollough

    Ok here is my entire project at the moment, ive made a few changes.

    Windows.cpp this is the windows code, I basically need all the functions and classes in the headers to be able to be used in Windows.cpp.



    #define WIN32_LEAN_AND_MEAN

    #include <windows.h>
    #include <windowsx.h>
    #include "IGE.h"
    #include "main.cpp"

    #define WINDOWSCLASS "WINCLASS1"

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)

    {
        PAINTSTRUCT ps;
        HDC            hdc;

        switch(msg)
        {
        case: WM_CREATE
            {
                CIGECORE            IGECORE;
                return(0);
            }break;
        case:WM_PAINT
             {
                 hdc = BeginPaint(hwnd,&ps);
                 EndPaint(hwnd,&ps);
                 return(0);
             }break;
            case WMDESTROY
            {
                PostQuitMessage(0);
                return(0);
            }break;
        default:break;
        }
        return(DefWindowProc(hwnd,msg.wparam,lparam));
    }

    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
                       LPSTR lpcmdline, int ncmdshow)
    {
        WNDCLASSEX winclass;
        HWND hwnd;
        MSG msg;

        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_HREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hinstance;
        winclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = WINDOWSCLASS;
        winclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);

        if(!RegisterClassEx(&winclass)){
            return(0);
        }

        if(!(hwnd = CreateWindowEx(NULL,WINDOWSCLASS,IGECORE.sAppTitle,WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            IGECORE.iPosX,IGECORE.iPosY,IGECORE.iWidth,IGECORE.iHeight,NULL,NULL,hinstance,NULL)))
        {
            return(0);
        }

        while(TRUE)
        {
            if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
            {
                if(msg.message == WM_QUIT)
                {
                    break;
                }
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            IGERUNTIME();
        }
    }

     


    Here is IGE.h this contains the CIGECORE class.


    class CIGECORE
    {
    public:
        LPSTR sAppTitle;
        int iPosX;
        int iPosY;
        int iWidth;
        int iHeight;
    };

     


    Here is main.cpp this contains the function that executes the game code. All the code the user types with be put into main.cpp into the function IGERUNTIME.



    void IGERUNTIME(){

    }

     


    ------ Build started: Project: Insane Game Engine, Configuration: Debug Win32 ------
    Compiling...
    Windows.cpp
    .\Windows.cpp(24) : error C2059: syntax error : ':'
    .\Windows.cpp(25) : error C2143: syntax error : missing ';' before '{'
    .\Windows.cpp(30) : error C2059: syntax error : ':'
    .\Windows.cpp(31) : error C2143: syntax error : missing ';' before '{'
    .\Windows.cpp(37) : error C2065: 'WMDESTROY' : undeclared identifier
    .\Windows.cpp(37) : error C2143: syntax error : missing ':' before '{'
    .\Windows.cpp(37) : error C2051: case expression not constant
    .\Windows.cpp(43) : error C2228: left of '.wparam' must have class/struct/union
            type is 'UINT'
    .\Windows.cpp(43) : error C2660: 'DefWindowProcA' : function does not take 3 arguments
    .\Windows.cpp(71) : error C2065: 'IGECORE' : undeclared identifier
    .\Windows.cpp(71) : error C2228: left of '.sAppTitle' must have class/struct/union
            type is ''unknown-type''
    .\Windows.cpp(72) : error C2228: left of '.iPosX' must have class/struct/union
            type is ''unknown-type''
    .\Windows.cpp(72) : error C2228: left of '.iPosY' must have class/struct/union
            type is ''unknown-type''
    .\Windows.cpp(72) : error C2228: left of '.iWidth' must have class/struct/union
            type is ''unknown-type''
    .\Windows.cpp(72) : error C2228: left of '.iHeight' must have class/struct/union
            type is ''unknown-type''
    main.cpp
    Generating Code...
    Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio\Projects\Insane Game Engine\Insane Game Engine\Debug\BuildLog.htm"
    Insane Game Engine - 15 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • j104

    So your project does not contain an int main() Perhaps you should start again with a project that contains an entry point If the code you posted is your entire project, I'm not surprised that things have gone awry.


  • slifinger

    That would be Gdi32.lib.  FWIW, I just googled GetStockObject, the first link is MSDN, and the MSDN page at the bottom tells you what to link to ( if you ever have these sort of troubles again ).  I tried a couple from your longer list, and they were all user32 ( to be honest, I expected you to need gdi32.lib, but the ones I tried and thought may be in there, were not ).


  • Ralf Boeck

    Here are the errors im getting with this code.

    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : error C2143: syntax error : missing ';' before '<class-head>'
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : error C2011: 'CIGECORE' : 'class' type redefinition
            c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : see declaration of 'CIGECORE'
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(18) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    main.cpp
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : error C2143: syntax error : missing ';' before '<class-head>'
    c:\documents and settings\owner\my documents\visual studio\projects\insane game engine\insane game engine\IGE.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    I started a blank project.

  • Using Classes and Structs