Forwrard References in MSVC6 vs. Borland 5.5 compilter tools.

Discussion in 'C++' started by John, Apr 4, 2004.

  1. John

    John Guest

    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
     
    John, Apr 4, 2004
    #1
    1. Advertising

  2. John

    John Carson Guest

    "John" <johnsnews(RemoveNoSpam)> wrote in
    message news:LjYbc.3162$
    > 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)
     
    John Carson, Apr 4, 2004
    #2
    1. Advertising

  3. John

    Leor Zolman Guest

    On Sun, 4 Apr 2004 14:18:20 -0400, "John"
    <johnsnews(RemoveNoSpam)> 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
     
    Leor Zolman, Apr 4, 2004
    #3
  4. 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.
     
    Kevin Goodsell, Apr 4, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Richardson
    Replies:
    1
    Views:
    324
    Victor Bazarov
    Dec 19, 2003
  2. Replies:
    2
    Views:
    429
  3. Bill Davy
    Replies:
    12
    Views:
    893
    Bill Davy
    Apr 22, 2005
  4. Bill Davy
    Replies:
    0
    Views:
    386
    Bill Davy
    Apr 19, 2005
  5. Replies:
    1
    Views:
    1,122
    SuperKoko
    Oct 2, 2006
Loading...

Share This Page