TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: PETER GARNER
date: 1997-07-28 01:16:00
subject: Mutually class definition

CP> I want to define two classes like the following:
CP> 
CP> class A
CP> {
CP>    A(int a) {  };
CP>    B func1() { return B(1); };
CP> }
CP> 
CP> class B
CP> {
CP>   B(int a) {  };
CP>   A func2() { return A(1); };
CP> }
CP> 
CP> two classes which each contains a function which return a instance of the 
other
CP> class. I get a "typename expected"-error in class A. I believe I have to 
use
CP> the "typename" keyword, but I can't get it to work.
CP> 
Hola Christian,
  You need to use forward declarations.  There is no 'typename' keyword 
in C++.  What the compiler is telling you is that it does not
recognise  the symbol as a typename, but it NEEDS a typename!  You need
to do this :
  
//  Forward Class Declarations
class A ;
class B ;
//  Actual Class Declarations
class A
{
   A(int a) ;
   B func1();
}
class B
{
  B(int a)  ;
  A func2() ;
}
//  From here on define your functions in a seperate C++ file or with
the  //  inline keyword in your header.
Best of Luck
Peter
---
 þ KWQ/2 1.2i þ ...had this been an actual emergency, we would have fled in 
terro
---------------
* Origin: St. Louis Users Group (1:100/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.