0

With this code:

#include <iostream>
using namespace std;

class point{    
public:
double get_x();
void  set_x(double v);

double get_y();
void  set_y(double z);

private:
double x, y;
};

point operator+(point& p1, point& p2)
{   
 point sum = {p1.x};// + p2.x, p1.y + p2.y};
 return sum;
}

int main()
{
 point a = {3.5,2.5}, b = {2.5,4.5}, c;
}

I get the following compiler errors saying the private members cannot be accessed:

point.cpp(22): error C2248: 'point::x': cannot access private member declared in class 'point' point.cpp(17): note: see declaration of 'point::x' point.cpp(8): note: see declaration of 'point'

I am pretty new to C++ and can't seem to figure out how to resolve this issue. Any help is much appreciated.

  • operator+ needs to be a friend of your point class to access the private. Related: [https://stackoverflow.com/questions/60698211/c-operator-overloading-with-friend-keyword](https://stackoverflow.com/questions/60698211/c-operator-overloading-with-friend-keyword) – drescherjm Jan 03 '22 at 21:38
  • [When should you use 'friend' in C++?](https://stackoverflow.com/questions/17434/when-should-you-use-friend-in-c) – 273K Jan 03 '22 at 21:39
  • Thanks for the answer. How would I make it a friend of my point class? – Matthew Yough Jan 03 '22 at 21:40
  • The second referenced question has the exact answer. – 273K Jan 03 '22 at 21:40
  • The members *are* private, so you are not supposed to access them. To get the value of `x`, you call `p1.get_x()`. That's the purpose of those "getters". – BoP Jan 03 '22 at 21:46
  • More general handy reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706) Note how Sbi implements the `+` operator by leveraging the `+=` operator. – user4581301 Jan 03 '22 at 21:48
  • 1
    Don't make `operator+` a friend. `point` has accessors that it can use; there's no need for it to be a friend. – Pete Becker Jan 03 '22 at 23:03

1 Answers1

0

You declared x and y as private so they can't be accessed in that function(or outside the class) unless you make that function a friend of the class, but if you want to overload the + operator, you should do it inside the class, something like this :

class point
{
public:
    point(double x, double y) // CONSTRUCTOR
        :x(x), y(y)
    {

    }
    double get_x() const{
        return x;
    };
    void set_x(double v)
    {
        x = v;
    };

    double get_y() const{
        return y;
    };
    void set_y(double z)
    {
        y = z;
    };

    point operator+(const point &obj)
    {
        double newX = this->x + obj.get_x();
        double newY = this->y + obj.get_y();

        point sum{newX, newY};
        return sum;
    };
private:
    double x, y;
};
  • Been a while since i used raw C++ tho, so I may be wrong on something –  Jan 03 '22 at 22:03
  • 1
    The only suggestions I have are to make `get_x()` and `get_y()` `const` member functions, to make `operator+` a const member function, and to change `operator+` to take its argument as a `const` reference. – Pete Becker Jan 03 '22 at 23:05
  • @PeteBecker I understand the part about making the argument of `operator+` const reference but why should I make get_x and y const –  Jan 03 '22 at 23:38
  • 3
    They should be `const` because they do not modify the object that they are applied to, so it's okay to call them on an object that is `const`. As currently written, `const post p; p.get_x();` isn't legal. If `get_x()` is `const` it's okay. If you make the argument to `operator+` const but don't make those member functions const you can't call them on that argument. – Pete Becker Jan 04 '22 at 00:02