[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Forward class declarations

Belebele

8/31/2008 8:23:00 PM

Why would the compiler give an error in the code below? Why would the
class declaration be required to compile the code below:

class Foo;
Foo& g();
void f()
{
g(); // use of undefined type 'Foo'
}

I am using VC++ 6.0 and VC++ 8.0

Thanks
6 Answers

peter koch

8/31/2008 8:31:00 PM

0

On 31 Aug., 22:22, Belebele <beluc...@gmail.com> wrote:
> Why would the compiler give an error in the code below? Why would the
> class declaration be required to compile the code below:
>
> class Foo;
> Foo& g();
> void f()
> {
>         g(); // use of undefined type 'Foo'
>
> }
>
> I am using VC++ 6.0 and VC++ 8.0
>
> Thanks

Because the compiler does not know how to destroy the result of g() -
an object of type Foo.

/Peter

Belebele

8/31/2008 8:53:00 PM

0

> Because the compiler does not know how to destroy the result of g() -
> an object of type Foo.

g returns a reference to an object of type Foo. The compiler should
not add code to call the destructor of the returned object.

peter koch

8/31/2008 9:23:00 PM

0

On 31 Aug., 22:53, Belebele <beluc...@gmail.com> wrote:
> > Because the compiler does not know how to destroy the result of g() -
> > an object of type Foo.
>
> g returns a reference to an object of type Foo. The compiler should
> not add code to call the destructor of the returned object.

True. I overlooked the ampersand. Sorry I can't help you - this looks
like a bug to me.

/Peter

Jim Z. Shi

9/1/2008 2:15:00 AM

0

Belebele wrote:
> Why would the compiler give an error in the code below? Why would the
> class declaration be required to compile the code below:
>
> class Foo;
> Foo& g();
> void f()
> {
> g(); // use of undefined type 'Foo'
> }

in my compiler, it complains that `undefined g()', a link-time error.
btw, my compiler is mingw-g++.

if you have defined g(), then it should depend on how you return the
result of g().

>
> I am using VC++ 6.0 and VC++ 8.0
>
> Thanks

Belebele

9/1/2008 3:52:00 PM

0

> in my compiler, it complains that `undefined g()', a link-time error.
> btw, my compiler is mingw-g++.

That tells me that the problem I had may be caused by a VC++ bug, or
rather, incomplete compliance to the standard.

> if you have defined g(), then it should depend on how you return the
> result of g().

It should not depend on anything. The returned value of g is a
reference and, as far as I know, reference and pointers should be
passed around regardless of the definition of the class of the
underlying object.

Jim Z. Shi

9/2/2008 5:39:00 AM

0



Belebele wrote:

> It should not depend on anything. The returned value of g is a
> reference and, as far as I know, reference and pointers should be
> passed around regardless of the definition of the class of the
> underlying object.
>
sure. the point is how you get that ref/ptr.

jim