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

  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
    28,412
    I am surprised that you managed to leave out the class keyword and still get it to compile. It looks illegal to me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:57 AM
  3. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 06: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