Friendship declaration?

This is a discussion on Friendship declaration? within the C++ Programming forums, part of the General Programming Boards category; I've got the following code: Code: #include <iostream> #include "Fraction.h" using namespace std; class Fraction_dup { friend class Fraction; public: ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Friendship declaration?

    I've got the following code:

    Code:
    #include <iostream>
    #include "Fraction.h"
    using namespace std;
    
    class Fraction_dup {
        friend class Fraction;
    public:
        void improper (Fraction x);
    };
    With the following Fraction class:

    Code:
    class Fraction {
        int top, bottom;
    
    public:
        Fraction(int a = 0, int b = 1);
        ~Fraction(){}
    
        //overloading operators
        void operator= (Fraction x);
    	Fraction operator+ (Fraction x);
    	Fraction operator- (Fraction x);
    	Fraction operator* (Fraction x);
    	Fraction operator/ (Fraction x);
    
        //function declarations
        void displayFraction();
        double toDecimal();
        Fraction toFraction(double x) const;
        int gcd(int a, int b);
    };
    But I keep getting the errors such as the following in the improper() definition:

    Code:
    error C2248: 'Fraction::top' : cannot access private member declared in class 'Fraction'|
    What did I do wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    It is the Fraction class that should declare Fraction_dup to be a friend. Why do you need Fraction_dup anyway?

    By the way, do not place using directives (and using declarations) at file scope in header files.
    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
    Jun 2005
    Posts
    6,299
    You made a mistake in assuming that friendship is mutual, or what direction it goes.

    Fraction_dup has declared Fraction as a friend. That means any member function of Fraction is allowed to access private members of Fraction_dup. However, Fraction_dup is not allowed to access private members of Fraction unless Fraction declares Fraction_dup as a friend.

    To put the logic of C++ friendship in human terms, if you declare me as a friend, that doesn't mean I will view you as a friend. It certainly doesn't mean I will grant you access to my privates.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    It is the Fraction class that should declare Fraction_dup to be a friend.
    Thanks!

    Why do you need Fraction_dup anyway?
    It's part of the assignment. EDIT: Scratch that, I was supposed to write a function, not a class.

    By the way, do not place using directives (and using declarations) at file scope in header files.
    Why?
    Last edited by 843; 04-10-2011 at 03:27 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    Quote Originally Posted by 843
    It's part of the assignment.
    What is Fraction_dup supposed to do? Also, why does it need to be a friend of Fraction? Why not just use the public interface provided by the Fraction class?

    Quote Originally Posted by 843
    Why?
    Every source file that includes your header will then have the using directive in effect, even if it is not wanted.
    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

  6. #6
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Quote Originally Posted by grumpy View Post
    You made a mistake in assuming that friendship is mutual, or what direction it goes.

    Fraction_dup has declared Fraction as a friend. That means any member function of Fraction is allowed to access private members of Fraction_dup. However, Fraction_dup is not allowed to access private members of Fraction unless Fraction declares Fraction_dup as a friend.

    To put the logic of C++ friendship in human terms, if you declare me as a friend, that doesn't mean I will view you as a friend. It certainly doesn't mean I will grant you access to my privates.
    Thanks! Got it

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Quote Originally Posted by laserlight View Post
    What is Fraction_dup supposed to do? Also, why does it need to be a friend of Fraction? Why not just use the public interface provided by the Fraction class?
    I was supposed to write a friend function, not a friend class. I just reread the assignment.

    Every source file that includes your header will then have the using directive in effect, even if it is not wanted.
    I see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class friendship.
    By nimitzhunter in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2010, 12:04 AM
  2. declaration
    By Tool in forum C Programming
    Replies: 3
    Last Post: 01-17-2010, 09:18 AM
  3. Odd declaration
    By LowlyIntern in forum C++ Programming
    Replies: 32
    Last Post: 01-16-2009, 07:06 AM
  4. *a++ declaration
    By tyskater in forum C Programming
    Replies: 13
    Last Post: 06-19-2004, 05:20 AM
  5. declaration
    By Clay in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-13-2001, 10:20 AM

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