This is a very typical problem - Programmers Heaven

Howdy, Stranger!

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

Categories

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.