When declaring freind classes, what happens without the class keywork?

This is a discussion on When declaring freind classes, what happens without the class keywork? within the C++ Programming forums, part of the General Programming Boards category; After reading the section on friend functions and claases, I know that when u declare a firend class, u must ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Question When declaring freind classes, what happens without the class keywork?

    After reading the section on friend functions and claases, I know that when u declare a firend class, u must cite explicitely:
    Code:
    class A ;  // forward declaration
    
    class B {
      friend class A ;
    
    public:
       ...
       ...
    } ;
    But would anyone please tell me the difference between the flollowing lines of code while defining class B? Thanks a lot!
    Code:
    class B {
      friend class A ;
    
    public:
       ...
       ...
    } ;
    
    class B {
      friend A ;
    
    public:
       ...
       ...
    } ;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    I am surprised that you managed to leave out the class keyword and still get it to compile. It looks illegal to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Question

    Although gcc v.4.1.0 issued an error while compiling, VC 2005 and RVDS works properly with this. Would u please kindly try compiling the attached file?

    I'm wondering now if it is a compiler dependent. I will try to find how different compilers treat it.
    Attached Files Attached Files

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    g++ 3.4.6 on Linux reports:
    main.cpp:45: error: a class-key must be used when declaring a friend

    From the C++ Standard (ISO/IEC 14882:2003) section 7.1.5:
    At least one type-specifier that is not a cv-qualifier is required in a declaration unless it declares a constructor, destructor or conversion function.

    My opinion is that this applies to friend declarations, so VC 2005 and RVDS happen to be lenient about it. Just use the class keyword for friend classes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    GCC 4.1.0 issued the same error msg.

    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. organizing classes in a game
    By MathFan in forum Game Programming
    Replies: 2
    Last Post: 04-28-2005, 09:23 AM
  2. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM
  3. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 05:58 PM
  4. Declaring Class function error?
    By Aidman in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2003, 08:27 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21