Friend function can't access private members

Hey,
I'm using friend function to overload the + operator but it keeps coming up with the error that the member variable is private "within this context".

I am developing the class to handle really large numbers

BigNumVect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef BIGNUMVECT_H_INCLUDED
#define BIGNUMVECT_H_INCLUDED
#include <vector>
#include <string>

using std::string;
using std::vector;

class BigNumVect
{
private:
    vector<int> m_bigNum_;

public:
    BigNumVect();
    BigNumVect(const string&);

    void printNumber();
    
    friend BigNumVect operator+(const BigNumVect&, const BigNumVect&);
};

#endif // BIGNUMVECT_H_INCLUDED 


BigNumVect.cpp
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
#include "BigNumVect.h"
#include <iostream>

using std::cout;

BigNumVect::BigNumVect()
{
    m_bigNum_.clear();
    m_bigNum_.push_back(0);
}

//Numbers stored in the vector are stored backwards i.e 1234 is stored [4][3][2][1]
BigNumVect::BigNumVect(const string& numStr)
{
    m_bigNum_.resize(numStr.length());

    for(int iii=0; iii<numStr.length(); iii++)
        m_bigNum_[iii]=int(numStr[numStr.length()-iii-1]-48);
}

void BigNumVect::printNumber()
{
    for(int iii=m_bigNum_.size()-1; iii>=0; iii--)
        cout << m_bigNum_[iii];
}

BigNumVect operator+(const BigNumVect& A, const BigNumVect& B)
{
    BigNumVect temp;

    int length=0;
    if(A.m_bigNum_.size()>=B.m_bigNum_.size())
        length = A.m_bigNum_.size();
    else
        length = B.m_bigNum_.size();

    temp.m_bigNum_.resize(length);
    int carry=0;
    int tempNum=0;
    for(int iii=0; iii<length; iii++)
    {
        tempNum= A.m_bigNum_[iii]+B.m_bigNum_[iii]+carry;

        carry=(A.m_bigNum_[iii]+B.m_bigNum_[iii])/10;

        temp.m_bigNum_[iii]=tempNum-10*carry;
    }
    if(carry!=0)
        temp.m_bigNum_.push_back(carry);

    return temp;
}
Your declaration should be:
friend BigNumVect& BigNumVect::operator+(BigNumVect&);

The syntax for declaring a friend function requires you to specify the scope correctly (i.e. the class with which the function is associated). In addition, you got the syntax for operator+ slightly wrong.
http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm
Last edited on
I have changed in now to
 
friend BigNumVect& BigNumVect::operator+(BigNumVect&);

in the header file

And
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
BigNumVect& operator+(BigNumVect& B)
{
    BigNumVect temp;

    int length=0;
    if(m_bigNum_.size()>=B.m_bigNum_.size())
        length = m_bigNum_.size();
    else
        length = m_bigNum_.size();

    temp.m_bigNum_.resize(length);
    int carry=0;
    int tempNum=0;
    for(int iii=0; iii<length; iii++)
    {
        tempNum= m_bigNum_[iii]+B.m_bigNum_[iii]+carry;

        carry=(m_bigNum_[iii]+B.m_bigNum_[iii])/10;

        temp.m_bigNum_[iii]=tempNum-10*carry;      
    }
    if(carry!=0)
        temp.m_bigNum_.push_back(carry);

    return temp;
}


In the cpp file but I still have the same errors.
Just make the function a class member.

header:
BigNumVect operator+(BigNumVect&);

implementation:
1
2
3
BigNumVect BigNumVect::operator+(BigNumVect& B)
{
. . .


Note: don't return a reference to a temporary.

Edit: had class scope in header.
Last edited on
Thanks for the advice about returning references, I've looked them up now and I understand why it's a bad idea. I guess for the mean time I will just implement as a class member.

I'm still curious though, does anyone know why the friend function method was not working?
I copied and pasted your code from the first post, and the only change i made was make operator+ return a reference to a BigNumVect, and it compiled just fine for me.

(well, i got 2 warnings which would need investigation (returning a reference to a local variable is bad). But i could access the private members fine).
Last edited on
Wow that works. Why does it work if you return a reference?
I copied and pasted your original code and it compiled without changes
well this is strange.
i removed the references and re-compiled and it's fine :o
So i agree with coder77777

I don't think you should return a reference for operaror+, so aplogies for leading you down the wrong path. See this:

http://stackoverflow.com/questions/2337213/return-value-of-operator-overloading-in-c
Last edited on
I've rewritten the code using friend functions after messing around with it and now it compiles. Now I have completely no idea why it wouldn't work in the first place.

I'm glad it works though ^^

Oh well, thanks guys!
Topic archived. No new replies allowed.