By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Friend functions error

JavierL
P: 17
Im doing an excercise, where i need to use a friend class and a friend function from another class to access a private member of a third class. With the 'friend class' everything when OK, but i have problems with the friend funcion, it would be very helpful if someone could see what's wrong because i cant, and im really stucked with this.

Expand|Select|Wrap|Line Numbers
  1. // C5_TP3.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <conio.h>  // for _getche(), VS 2010 closes the cmd >_<
  7.  
  8.  
  9. using namespace System;
  10. using namespace std;
  11.  
  12. class A; // for refering to A* in class C
  13.  
  14. class C {
  15.     void i(A*);
  16. };
  17.  
  18. class A {
  19. public :
  20.     friend class B; // Friend class B
  21.     friend void C::i(A*); // Friend function from C, this is what is giving me issues
  22. private:
  23.     string a;
  24. };
  25.  
  26. class B{
  27. public:
  28.     void a(A* x);
  29. };
  30.  
  31. void B::a(A* x){
  32.     x->a= "Reporting in B friend";
  33.     cout << x->a << endl;
  34.     _getche();
  35. }
  36.  
  37. void C::i(A* k){
  38.     k->a = "Reporting in C friend";
  39.     cout << k->a << endl;
  40.     _getche();
  41. }
  42.  
  43. int main(array<System::String ^> ^args)
  44. {
  45.     A z; B b; C c;
  46.     b.a(&z);
  47.     c.i(&z);
  48.     return 0;
  49. }
Here is the output error from vs 2010:

1>C5_TP3.cpp(22): error C2248: 'C::i' : cannot access private member declared in class 'C'
1> C5_TP3.cpp(16) : see declaration of 'C::i'
1> C5_TP3.cpp(15) : see declaration of 'C'
1>C5_TP3.cpp(53): error C2248: 'C::i' : cannot access private member declared in class 'C'
1> C5_TP3.cpp(16) : see declaration of 'C::i'
1> C5_TP3.cpp(15) : see declaration of 'C'
Oct 12 '10 #1

✓ answered by hype261

Your errors are not due to the friend function. Your errors are due to your class C

Expand|Select|Wrap|Line Numbers
  1. class C 
  2.     void i(A*); 
  3. };
C++ defaults to private scope for classes. So when you call c.i(&z); in your main function it is giving you an error.

Change code to

Expand|Select|Wrap|Line Numbers
  1. class C
  2. {
  3. public:
  4. void i(A*);
  5. };
  6.  

Share this Question
Share on Google+
3 Replies


100+
P: 687
I don't think that you can access some private member x::p of a class X by declaring X and/or X::p your friend. Another way - make X access your private function by declaring X your friend - yes.
Oct 13 '10 #2

100+
P: 207
Your errors are not due to the friend function. Your errors are due to your class C

Expand|Select|Wrap|Line Numbers
  1. class C 
  2.     void i(A*); 
  3. };
C++ defaults to private scope for classes. So when you call c.i(&z); in your main function it is giving you an error.

Change code to

Expand|Select|Wrap|Line Numbers
  1. class C
  2. {
  3. public:
  4. void i(A*);
  5. };
  6.  
Oct 13 '10 #3

JavierL
P: 17
Yes! I knew it was something dull that I was missing, thank you, very much.
Oct 14 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.