By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,132 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,132 IT Pros & Developers. It's quick & easy.

Forwrard References in MSVC6 vs. Borland 5.5 compilter tools.

P: n/a

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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"John" <johnsnews(RemoveNoSpam)11**@hotmail-RemoveNoSpam.com> wrote in
message news:Lj*******************@news20.bellglobal.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2

P: n/a
"John" <johnsnews(RemoveNoSpam)11**@hotmail-RemoveNoSpam.com> wrote in
message news:Lj*******************@news20.bellglobal.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
John wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6

P: n/a
John wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.