Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

This is a very typical problem

ColacXColacX Posts: 141Member
This is how I want my code to look.

[code]object* everything[80][24];

class object
{
void moveleft(){};
};[/code]

Ofcourse the class "object" must be defined before i define the pointerarray "everything" which cause this code to give of a compiler error. In java I wouldn't have to worry about this, but how do I do this in c++?

This problem is very common does it have a name?

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : This is how I want my code to look.
    :
    : [code]: object* everything[80][24];
    :
    : class object
    : {
    : void moveleft(){};
    : };[/code]:
    :
    : Ofcourse the class "object" must be defined before i define the
    : pointerarray "everything" which cause this code to give of a
    : compiler error. In java I wouldn't have to worry about this, but how
    : do I do this in c++?
    :
    : This problem is very common does it have a name?
    :

    Split it up into a header and a cpp file (which you should do in any case - it's proper coding).
    Then, just make sure you include the header before defining anything else (which again is what you should do in any case).

    There is a way to forward define something, but it's not very neat:
    [code]
    class object;

    object* something[80][24];

    class object
    {
    ...
    };
    [/code]

    But in this case I would not advise it, because you should put all the variables in a cpp file.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • ColacXColacX Posts: 141Member
    : : This is how I want my code to look.
    : :
    : : [code]: : object* everything[80][24];
    : :
    : : class object
    : : {
    : : void moveleft(){};
    : : };[/code]: :
    : :
    : : Ofcourse the class "object" must be defined before i define the
    : : pointerarray "everything" which cause this code to give of a
    : : compiler error. In java I wouldn't have to worry about this, but how
    : : do I do this in c++?
    : :
    : : This problem is very common does it have a name?
    : :
    :
    : Split it up into a header and a cpp file (which you should do in any
    : case - it's proper coding).
    : Then, just make sure you include the header before defining anything
    : else (which again is what you should do in any case).
    :
    : There is a way to forward define something, but it's not very neat:
    : [code]:
    : class object;
    :
    : object* something[80][24];
    :
    : class object
    : {
    : ...
    : };
    : [/code]:
    :
    : But in this case I would not advise it, because you should put all
    : the variables in a cpp file.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    Thanks the later one works. But I don't like splitting a program into multiplefiles. The class code isn't going to be used anywhere else anyway.
  • MT2002MT2002 Posts: 1,444Member
    : Thanks the later one works.

    I should probably note that it is not "forward defining" anything, its forward declaring it. Because it is only declared, you cannot use it within an expression (Like try to access a data member) without causing an error.

    Remember that declaring something is different then defining it.

    : But I don't like splitting a program
    : into multiplefiles. The class code isn't going to be used anywhere
    : else anyway.

    If you ever need to work on a large project, I would highly recommend to get used to this.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.