forward declaration related problem

From: exits funnel (exitsfunnel_at_NOSPAMyahoo.com)
Date: 11/30/03


Date: Sat, 29 Nov 2003 23:43:45 GMT

Hello,

I have the following code:

class A
{
   public:
     A(int i = 0):asInt(i) { }
     operator B( ) { return B(asInt); }
   private:
     int asInt;
};
class B
{
   public:
     B(int i = 0):bsInt(i) { }
     operator A( ) { return A(bsInt); }
   private:
     int bsInt;
};
int main( )
{ }

When I try to compile it my compiler has this to say:
> test.cpp:5: parse error before `('
> test.cpp:5: syntax error before `('
> test.cpp:7: semicolon missing after declaration of `A'
> test.cpp:7: parse error at null character
> test.cpp: In method `A::A (int)':
> test.cpp:4: class `A' does not have any field named `asInt'
> test.cpp: At top level:
> test.cpp:7: parse error at null character

The problem is obviously that when the compiler hits the reference to B
in A it doesn't yet know anything about A. So I add this:

class B;

to the very beginning of the file and now my compiler rebuts with:
> test.cpp: In method `A::operator B ()':
> test.cpp:7: return type `class B' is incomplete
> test.cpp:7: invalid use of undefined type `class B'
> test.cpp:1: forward declaration of `class B'

I understand the issue here, but I'm not sure how to solve it. If
anyone could point me in the right direction I'd really appreciate it.
Thanks in advance.

-exits



Relevant Pages

  • Re: C++ in ternms of C
    ... int b, c, d; ... To consider the other aspects of OOP, we obviously have public, private and ... In short, imagine the compiler ripping all of the functions out of a class, ... I tried to analise the ASM file produ ced my the VC++ compil er. ...
    (microsoft.public.vc.language)
  • Re: question
    ... >class A(const int l) ... show us the same code that your compiler saw if you tell us about an error ... so it wouldn't accept a subsequent definition of an A object ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Why does compiler only look at public methods of superclasses of...?
    ... Compiler enumerates all methods in class A and all public methods ... in the superclasses of A to find methods named x ... if method is declared final, static or private then compiler knows ... override a protected method. ...
    (comp.lang.java.programmer)
  • Re: Some Qs related to VTable and DialogBox!
    ... int main ... Does the compiler might have created VTable for Class B or ... Does virtual functions have any effect wiht access specifiers? ... if you make A::VFuncprivate, ...
    (microsoft.public.vc.mfc)
  • Question about use and host association
    ... IMPLICIT NONE ... PRIVATE ... SUBROUTINE mytype_Write ... Is this compiler message bogus, or am I misunderstanding something about use and/or host ...
    (comp.lang.fortran)