I have a very simple class

class Point
{

public:
Point();
Point(double x, double y);
  double x_;
double y_;
}

the problem is, I can't assign my class to another instance, ie:

Point A(2,4);
Point B();
B = A;

It says the lvalue must be modifiable. Why is it not?

Thanks,

Dave

Try this.

class Point {
public:

   double x;
   double y;

   Point() {
      x = 0;
      y = 0;
   }

   Point(double _x, double _y);
      x = _x;
      y = _y;
   }

   Point &operator =(const Point &p) {
      x = p.x;
      y = p.y;
      return *this;
   }
};

Point B(); is the declaration of a function called 'B' which takes no arguments and returns a Point.
modify to Point B ; and you have the definition of a variable called 'B' which is of type Point (initialized via the default constructor).

ahhh that was the key ! Needed to use B instead of B()

Thanks!

Point B(); is the declaration of a function called 'B' which takes no arguments and returns a Point.
modify to Point B ; and you have the definition of a variable called 'B' which is of type Point (initialized via the default constructor).

Are you completely sure about this?
As far as i know if you have declared the Point class' constructor Point() and then you state Point B(); then B is an instance of Point instantiated via the default constructor.
I also thought that that if you declare the copy constructor the assignation B = A should propery work

class Point{
private:
  int x_;
  int y_;
public:
  Point(){/* whatever*/}
  Point(int x,int y){/* whatever*/}
  Point(const Point& p){ 
     x_ = p.x_;
     y_ = p.y_;
  }
};

I consider myself a begginer and I learn as I go. Then this you posted doesn't seems to be correct for me. Pleas correct me.
Kind regards

Are you completely sure about this?
As far as i know if you have declared the Point class' constructor Point() and then you state Point B(); then B is an instance of Point instantiated via the default constructor.
I also thought that that if you declare the copy constructor the assignation B = A should propery work

class Point{
private:
  int x_;
  int y_;
public:
  Point(){/* whatever*/}
  Point(int x,int y){/* whatever*/}
  Point(const Point& p){ 
     x_ = p.x_;
     y_ = p.y_;
  }
};

I consider myself a begginer and I learn as I go. Then this you posted doesn't seems to be correct for me. Pleas correct me.
Kind regards

Yes, vijayan121 is correct. Try this program below and see what happens:

#include <iostream>
using namespace std;

class Point {
public:

   double x;
   double y;

   Point() 
   {
      cout << "I'm in the constructor\n";
      x = 0;
      y = 0;
   }

   Point(double _x, double _y)
   {
      x = _x;
      y = _y;
   }

   Point &operator =(const Point &p) 
   {
      x = p.x;
      y = p.y;
      return *this;
   }
};



int main ()
{
    Point B();  // nothing displayed
    Point C; // constructor call
    B();     // function call
    Point D (); // nothing displayed
    return 0;
}


Point B ()
{
      cout << "I'm in the function\n";
      Point* A = new Point ();  // constructor call
      return *A;
}

Thanks a lot!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.