'friend' functions not accessing private members

We're learning about friend functions and vectors, the ladder I seem to of nailed on the first go. But, now I can't get my friend functions to access private members of the same class. I thought that was the advantage to being a 'friend'?

Anyway, here's the source code:

Main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <iostream>
#include <vector>
using namespace std;

#include "Checking.h"
#include "Savings.h"

int
main(void)
{
      double cB, sB;
      int cA=0, sA=0;
      vector <Checking *> check( 50 );
      vector <Saving *> save( 50 );   
      

      cout << "Enter Checking Balance for account #" << cA+1 << ":";
      cin >> cB;
      check[cA] = new Checking( cB , cA+1 );
      ++cA;      
      
      cout << "\nEnter Savings Balance for account #" << sA+1 << ":";
      cin >> sB;
      save[sA] = new Saving( sB , sA+1 );
      ++sA;



//      check.printC();
//      save.printS();
      
      system("pause");
      return(0);
}


.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "Checking.h"


Checking::Checking(double bC, int aN)
{
         setBalanceC(bC);
         setAcct(aN);
}


// How do I get this to access BalanceC, a private member?
std::ostream& operator<<( std::ostream &output, Checking &temp )
      {output << temp.BalanceC;
      return output;};
      

void Checking::setBalanceC(double bC)
{
          BalanceC=bC;
}

double Checking::getBalanceC()
{
          return BalanceC;
}

void Checking::setAcct(int aN)
{
          Acct=aN;
}

int Checking::getAcct()
{
          return Acct;
}

void Checking::printC()
{
          cout << "Checking: " << endl;
          cout <<  "     Balance is: " << BalanceC << endl;
          cout <<  "     Acct is: " << Acct << endl;
}



.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Checking
{
     // This is where I think there's a problem, but I don't know what
      // can't access private member, despite being
      // a 'friend'
      friend std::ostream& operator<<( std::ostream& output, const Checking& temp);
      public:
             Checking(double, int);
             void setBalanceC(double);
             double getBalanceC();
             void setAcct(int);
             int getAcct();
             void printC();
      private:
              double BalanceC;
              int Acct;
};


In short: how do I get a friend function to access a private member. My vectors and operator overload probably aren't perfect either, ANY help would be greatly appreciated.

Should of posted these earlier, Sorry!

...\Checking.h In function `std::ostream& operator<<(std::ostream&, Checking&)':

Line 16: ...\Checking.h `double Checking::BalanceC' is private

Line 22 ...\Checking.cpp within this context
Last edited on
You did not define the friend function. Its declaration is

friend std::ostream& operator<<( std::ostream& output, const Checking& temp );

But you defined overload function with the same name instead of the friend function

std::ostream& operator<<( std::ostream &output, Checking &temp )
Last edited on
The prototype

friend std::ostream& operator<<( std::ostream& output, const Checking& temp);

does not match the implementation

std::ostream& operator<<( std::ostream &output, Checking &temp )
Ok I fixed them both to:

friend std::ostream& operator<<( std::ostream& output, const Checking& temp);

&

std::ostream& operator<<( std::ostream &output, Checking& temp )

But I'm still getting the same exact error, which I realize i did [not] post earlier:

...\Checking.h In function `std::ostream& operator<<(std::ostream&, Checking&)':

Line 16: ...\Checking.h `double Checking::BalanceC' is private

Line 22 ...\Checking.cpp within this context
Last edited on
you didn't change anything hence the error won't go away. Change it to:

friend std::ostream& operator<<( std::ostream& output, const Checking& temp);

std::ostream& operator<<( std::ostream &output, const Checking &temp )

they must indeed look identically
Wow, can't believe i missed that. Thank you for the second pair of eyes, it fixed the issue. =D
Now when I test my BalanceC value from the Checking class, I'm getting garbage values using ".at(0)". For now I'm only working with the 0th value in the Checking vector, is there a better way to print an element of a vector?

I was getting this same issue before I ran into the 'operator<<'/'friend function' issue, and I thought that would of been the fix(thought it was multiplying my Acct and BalanceC numbers and returning on int or something).

So basically, what's the best way to print the nth element of a vector?
So basically, what's the best way to print the nth element of a vector?
Using the size() function of the vector:
1
2
3
4
5
6
vector <Saving *> save( 50 );
...
for(int i = 0; i < save.size(); ++i)
{
  // do something with save[i]
}
in other words: make sure that n is within the range of the vector

That you get garbage when accessing an element of the vector can have another reason:
The object ".at(0)" is invalid somehow.
Topic archived. No new replies allowed.