473,513 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend functions error

JavierL
17 New Member
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
3 1941
newb16
687 Contributor
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
hype261
207 New Member
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
17 New Member
Yes! I knew it was something dull that I was missing, thank you, very much.
Oct 14 '10 #4

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

Similar topics

5
8946
by: Oplec | last post by:
Hi, I am trying to figure out the correct syntax for declaring a friend function in a class template and then defining that member after the class. The code example below is what I am trying to get to work properly; it generates a link error. Thank you, Oplec. #include <cstdlib>
4
1537
by: Hans De Winter | last post by:
Why does my compiler complain with the message "`int*My::Test::_shape' is private" when I try to compile the following code? It seems it has something to do with the namespace since when I leave that out then everything works fine. Many thanks for your help. Hans // START CODE #include <iostream>
6
2323
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
9
2516
by: Neelesh | last post by:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends") where it is mentioned that What I observe is a bit different - it is fine that there is an error for Xform, but the reason is not that Xform is not in scope - the reason is that the class's size is not known. Also, the second line is compiling fine - without any problems....
2
7906
by: Gerard Stafleu | last post by:
I have a project with the following module: Module mdlDeclarations Public Enum getMethod gmFirst gmLast End Enum End Module and the following class:
1
1498
by: gw7rib | last post by:
Suppose I have a function, whose protoype is somewhat complicated, for example, with some #defines and typedefs, it looks like this: INT_PTR CALLBACK PropDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); and I'm not allowed to make it a member function <OTbecause it's a dialog box function </OT>. I want to make it a...
1
2022
by: curious2007 | last post by:
Hello everybody, When I try to declare nontemplate friend functions I get the following warning: friend decleration......declares a non-template function (if this is not what you intended, make sure the function tempate has already been declared and add<> after the function name here)-Wno-non-template-friend disables this warning. ...
3
2462
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
1
212
by: nooriddeen88 | last post by:
is it possible for a "friend" function to return a "this" pointer?
3
1715
by: mojmir | last post by:
hello, i have problem with following code (see below). compiler (vs2005sp1) complains that the call to fn is ambiguous: error C2668: 'BB::fn' : ambiguous call to overloaded function could be 'void BB::fn<dummy>(T &,BB::foo &)' or 'void AA::fn<dummy>(T &,BB::foo &)' okay, i have said, and tried to be more specific about the namespace
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.