Friend Functions

This is a discussion on Friend Functions within the C++ Programming forums, part of the General Programming Boards category; I am having problems with something that should be simple. Just trying to initialize a member variable and then use ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    19

    Friend Functions

    I am having problems with something that should be simple. Just trying to initialize a member variable and then use a friend function to display the private member variable. As you can see, I pass in a number through int x, then try to initialize private member variable to that in the constructor and then display it but I still cannot access it. Any ideas on what I am doing wrong. I know this is a simple program but I would rather learn it this way then apply it to my school program.

    Code:
    #include <iostream>
    using namespace std;
    
    
    class MyClass
    {
    	int a;		
    	
    public:				
    	MyClass::MyClass(int x) 
    	{
    		a=x;													
    	}	
    	friend void display();
    };
    
    void display(MyClass number)
    {
    	cout << number.a << "\n";
    
    }
    
    main()
    {
    	
    	MyClass number(200);
    	display();
    	return 0;																			
    }

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Code:
    friend void display();
    void display(MyClass number)
    display isn't the same friend function you declared in the class. You need to add the parameter in the declaration too.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    You're declaring a function named display(), which accepts no arguments, as a friend but not defining it. The function you are defining, display(MyClass) which accepts one argument of type MyClass, is not a friend of MyClass.

    Try this;
    Code:
    #include <iostream>
    using namespace std;
    
    
    class MyClass
    {
    	int a;		
    	
    public:				
    	MyClass::MyClass(int x) 
    	{
    		a=x;													
    	}	
    	friend void display(MyClass);
    };
    
    void display(MyClass number)
    {
    	cout << number.a << "\n";
    
    }
    
    main()
    {
    	
    	MyClass number(200);
    	display(number);
    	return 0;																			
    }

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    That actually worked. Let me see if I can adjust my actual program. Thank you.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Quote Originally Posted by rculley1970
    That actually worked.
    I'm overcome with surprise. Here we were, just typing away at random. And, by some amazing coincidence, the random letters we typed in happened to offer a solution to your problem.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    ^^ Saracasm is the lowest form of wit grumpy. But I like your witty reply
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  2. Friend functions
    By ForlornOdium in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2003, 09:25 PM
  3. Friend template functions
    By lyx in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 01:11 PM
  4. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:37 AM
  5. Visual C++ and friend functions giving errors
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2001, 05:55 PM

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