Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A little C++ help

Options
  • 05-07-2010 12:07pm
    #1
    Registered Users Posts: 33


    Hi,

    I'm starting to get a little familiar with C#, but I haven't a Scooby Doo with C++. :(

    I have a task to add two structs to an MFC C++ ActiveX control.

    My normal route is to right-click on the project, add a new class, and then replace the class keyword with struct.

    When I do that in C++, two files get created - a .h and a .cpp
    Is this okay so far, or have I done something wrong?

    Here is the code generated:
    //DVALUE.h
    #pragma once

    struct DVALUE
    {
    DECLARE_DYNAMIC(DVALUE)

    public:
    DVALUE();
    virtual ~DVALUE();

    protected:
    DECLARE_MESSAGE_MAP()
    };



    //DVALUE.cpp
    #include "stdafx.h"
    #include "CWCControl.h"
    #include "DVALUE.h"

    IMPLEMENT_DYNAMIC(DVALUE, CWnd)

    DVALUE::DVALUE()
    {

    }

    DVALUE::~DVALUE()
    {

    }


    My question is, where do I put the members? e.g. double value;
    In the .h or .cpp file?

    Thanks,
    Slap


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Where ever you define your class/struct, so in this case in the header file.


  • Registered Users Posts: 981 ✭✭✭fasty


    Does that code even compile when you include it in your project?

    The IMPLEMENT_DYNAMIC(DVALUE, CWnd) line is a macro that implements some functions MFC needs. It implies that the class is derived from a CWnd. Furthermore the same goes for the DECLARE_MESSAGE_MAP() macro. a message map in MFC maps windows messages to methods.


  • Registered Users Posts: 33 slap


    Nope, it doesn't compile.

    LNK2001: unresolved external symbol at "protected: virtual struct AFX_MSGMAP const* _thiscall DVALUE::GetMessageMap(void)const "

    LNK1120: 1 unresolved externals


    Not sure what is wrong. I only want to add a struct. Did I do something wrong when creating the struct? Is there some other way of doing this? :confused:

    Thanks,
    Slap


  • Registered Users Posts: 981 ✭✭✭fasty


    delete the following lines:

    DECLARE_DYNAMIC(DVALUE)
    DECLARE_MESSAGE_MAP()
    IMPLEMENT_DYNAMIC(DVALUE, CWnd)

    These things are MFC macros that generate some functions the framework needs as I mentioned in my above post. They aren't a requirement of a C++ class or struct. BTW, in C++ a struct is just a class with all public members by default.


  • Registered Users Posts: 33 slap


    Cool, thanks.

    So how do I use that in my application now?
    Inside the app.h file I've put:

    struct DVALUE dVal;

    And then inside the app.cpp file I've put:
    dVal.val = 25;
    dVal.unc = 39; // etc.

    Throwing up errors though:
    C2079:'dVal' uses undefined struct 'DVALUE'
    C2039: 'val' : is not a member of System::Int32 // etc.

    Just to reiterate, I don't know *anything* about C++. This all seems weird to me.
    Oh, and val is of type double, not Int.

    Confused.com :confused:

    Thanks,
    Slap


  • Advertisement
  • Registered Users Posts: 981 ✭✭✭fasty


    Confused.com!?! Ugh!

    Anyway, you need to include the header to use the structure. I see you have an app.h file... Include the header there and see how you get on!

    Also, you just declare dVal like this:
    DVALUE dVal;
    

    It's not really a problem that you don't know any C++ as long as you try to learn it yourself! Having to deal with MFC code isn't going to make it easy for you though... I would suggest some basic C++ tutorials!


Advertisement