Thread: Friendship declaration?

  1. #1
    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
    28,413
    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.
    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
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    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
    28,413
    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.
    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

  6. #6
    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
    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, 01:04 AM
  2. declaration
    By Tool in forum C Programming
    Replies: 3
    Last Post: 01-17-2010, 10:18 AM
  3. Odd declaration
    By LowlyIntern in forum C++ Programming
    Replies: 32
    Last Post: 01-16-2009, 08: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