Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Forwrard References in MSVC6 vs. Borland 5.5 compilter tools.

Reply
Thread Tools

Forwrard References in MSVC6 vs. Borland 5.5 compilter tools.

 
 
John
Guest
Posts: n/a
 
      04-04-2004

MSVC6 (SP5) gives this error when I try to use a non-pointer type to a class
that is forward-referenced:
reo.cpp( : 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


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-04-2004
"John" <johnsnews(RemoveNoSpam)(E-Mail Removed)> wrote in
message news:LjYbc.3162$(E-Mail Removed)
> MSVC6 (SP5) gives this error when I try to use a non-pointer type to
> a class that is forward-referenced:
> reo.cpp( : 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)

 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-04-2004
On Sun, 4 Apr 2004 14:18:20 -0400, "John"
<johnsnews(RemoveNoSpam)(E-Mail Removed)> wrote:

>
>MSVC6 (SP5) gives this error when I try to use a non-pointer type to a class
>that is forward-referenced:
> reo.cpp( : 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
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-04-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
QUERY: Borland C++ 4.5 vs Borland Turbo C++ 1.01 dhruba.bandopadhyay@hotmail.com C++ 1 10-02-2006 10:57 AM
(Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib, winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 12 04-22-2005 12:19 PM
RE: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 0 04-19-2005 09:27 AM
spirit 1.6.0 stlport 4.6.2 msvc6 programmer_pete@yahoo.com C++ 2 01-27-2005 05:23 PM
template specialization hides other specializations-msvc6 Tom Richardson C++ 1 12-19-2003 05:05 PM



Advertisments