| | |
My class is not a modifiable lvalue?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
I have a very simple class
the problem is, I can't assign my class to another instance, ie:
It says the lvalue must be modifiable. Why is it not?
Thanks,
Dave
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
Point A(2,4); Point B(); B = A;
It says the lvalue must be modifiable. Why is it not?
Thanks,
Dave
•
•
Join Date: Mar 2008
Posts: 1,431
Reputation:
Solved Threads: 118
Try this.
C++ Syntax (Toggle Plain Text)
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; } };
•
•
Join Date: Aug 2008
Posts: 11
Reputation:
Solved Threads: 1
•
•
•
•
Point B();is the declaration of a function called 'B' which takes no arguments and returns a Point.
modify toPoint B ;and you have the definition of a variable called 'B' which is of type Point (initialized via the default constructor).
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
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Visual C++ 2008 - Printing Richtextbox control
- Next Thread: static members problem!!!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






