Forwrard References in MSVC6 vs. Borland 5.5 compilter tools.

J

John

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.

TIA,
--John
 
J

John Carson

John said:
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.

TIA,
--John


Microsoft is right. The code shouldn't compile. Perhaps Borland is letting
you get away with it because fn doesn't actually do anything with its B
argument. Change the code.
 
L

Leor Zolman

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
 
K

Kevin Goodsell

John said:
void main() {;}
/* ----------------------------- */

Additional note: main() must return int, even if your compiler
mistakenly allows void. This is explicitly required by the standard.
void is not and never has been an acceptable return type for main().

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top