1

I have two classes:

class CALLDB;
class CALL
{
friend class CALLDB;
public:
    string GetStart()const;
private:
    string start;
};

And second class:

class CALLDB
{
friend class CALL;
public:
unsigned int Load(istream& fin);
private:
unsigned int numCalls;
};

In the main function, I did this:

int main(){
CALLDB calldata;
cout<<calldata.numCalls;
}

And then it says:

error C2248: 'CALLDB::numCalls': cannot access private member declared in class 'CALLDB'

Why does it happen? Is something wrong with my friend class declaration?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Tianyi Liu
  • 31
  • 1
  • 4
  • 6
    `numCalls` is not friends with `main`... – ta.speot.is Feb 12 '18 at 03:38
  • You are trying to access ``calldata.numCalls`` from main so obviously it will result in compile-time error – Asesh Feb 12 '18 at 03:44
  • oh thx, I see.. so if I don't make any change on those 2 classes, what should I do to the main? – Tianyi Liu Feb 12 '18 at 03:50
  • 1
    If you don't change anything, there is nothing you can do. – O'Neil Feb 12 '18 at 03:55
  • 2
    You could add `friend int main();` into your CALLDB class declaration; that would allow your main() function to see its private member variable. That said, a better approach would be to add a getter-method like `int getNumCalls() const {return numCalls;}` to your CALLDB class instead, and then main() can just call `calldata.getNumCalls();` instead of trying to access the private variable directly. – Jeremy Friesner Feb 12 '18 at 04:02
  • @JeremyFriesner damn THANKS dear sir! enlightened me like master Wugui – Tianyi Liu Feb 12 '18 at 04:10

1 Answers1

0

friend class and functions are allowed to access the private data members of a class. so inside the class which is made as friend to another class,you can access the private member.to access the private members inside the friend class or friend function we have to create object for that class and then only we can access the private members. in your example you made CALLDB as friend to CALL and CALL to CALLDB so both the classes can access the private members of other class (i.e) you can access the private member of CALLDB in CALL and private member of CALL in CALLDB.but you tried to access the private members from main function which is not a friend to a class.I hope that you understand the concept.here i have given an simple example to understand the concept of friend class .you try that and do your program as per your requirement

#include <iostream>
Using namespace std;
class CALLDB;
class CALL
{
  friend class CALLDB;
  private:
    void display()
      {

       cout<<"\n from Private function of display() of the class CALL ";
     }
};

class CALLDB
  {
    friend class CALL;
    public:
       void output()
         {
           CALL ca;
           cout<<"\n from public function output of CALLDB class ";
           cout<<"\n Calling of private function display of class CALL";
           ca.display();
        }
  };

int main()
 {
    CALLDB cd1;
    cd1.output();
 }

OUTPUT

from public function output of CALLDB class
Calling of private function display of class CALL
from Private function of display() of the class CALL

K.Vani
  • 72
  • 4