Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Program

Forward Class Declaration

This topic is 5473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't get this example to work:

class Bottle;

class Carton
{
    public:
        Carton(Bottle bottle)
        {
                height = bottle.height;
                length = 4.0 * bottle.diameter;
                width = 3.0 * bottle.diameter;
        }
    
    private:
        double length, width, height;
};

class Bottle
{    
    public:
        Bottle(double h, double d)
        {
            height = h;
            diameter = d;
        }
         friend Carton::Carton(Bottle bottle);
        
    private:
        double height;
        double diameter;
};  

[\code]

I try putting a declaration of Bottle before defining the class so Carton can use it, but the compiler(Dev C++ 4.9.8.0) says forward declaraton of 'struct bottle' and bottle as incomplete type.

If I put the definition of Bottle before Carton it say's 'Carton is neithre function nor member function' and more errors. I tried putting them in different source files, different orders with forward declaration and without.I can't seem to get it to work.
    

[edited by - Program on October 21, 2003 11:35:36 PM]   

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You can''t do it that way, you can only make pointers to a forward declaration:

class myclass1;

class myclass2
{
protected:
myclass1* m1; // legal

myclass1 m2; // not legal

};

class myclass1
{
};

Share this post


Link to post
Share on other sites
You are declaring it... but until it is defined, the compiler won''t know how much space must be set aside for it... hence the class which uses it as a data member''s size cannot be evaluated.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!