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'?
#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
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?