On Sun, 4 Apr 2004 14:18:20 -0400, "John"
<johnsnews(RemoveNoSpam)11**@hotmail-RemoveNoSpam.com> wrote:
MSVC6 (SP5) gives this error when I try to use a non-pointer type to a class
that is forward-referenced:
reo.cpp(8) : error C2027: use of undefined type 'B'
However Borland's free 5.5 compiler successfully compiles the same code
snippet.
/* ----------------------------- */
class B; // forward reference
// class A references B
class A
{
public:
void fn(B b) {} // ##### MSVC6 gives error, whereas Borland does not
#####
};
// class B references A
class B
{
public:
void fn(A a) {}
};
void main() {;}
/* ----------------------------- */
The Microsoft compiler will work if I alter the code by changing the
parameters into pointers (seems the forward ref works with pointers only).
However Borland's forward references work with either way.
I have to assume there is a way to make this work in MSVC6 without altering
the code.
Your suggestions and any helpful pointers (no pun) would be appreciated.
John Carson is right; Borland allows this, but it is not legal C++. By the
time his post showed up I'd already modified your test program to
demonstrate all it takes to make Borland admit defeat, so here it is:
#include <iostream>
using namespace std;
class B;
class A
{
public:
void fn(B b) { b.speak(); }
};
class B
{
public:
void fn(A a) {}
void speak() { cout << "Woof!" << endl; }
};
int main() { return 0;}
Note: C and C++ are compiled conceptually (if not actually) in one pass, so
in general you can never expect the language to let you use a type in a
manner that would require a compiler to know the size of an object before
it has seen enough information to possibly be able to determine that size.
Thus pointers and references to incomplete types are accepted (since the
only thing the compiler really needs to know is the size of a pointer or
reference.)
-leor
--
Leor Zolman --- BD Software ---
www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html