Archived

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

Program

Forward Class Declaration

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
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