943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 897
  • C++ RSS
Aug 14th, 2008
0

My class is not a modifiable lvalue?

Expand Post »
I have a very simple class

C++ Syntax (Toggle Plain Text)
  1. class Point
  2. {
  3.  
  4. public:
  5. Point();
  6. Point(double x, double y);
  7. double x_;
  8. double y_;
  9. }

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

C++ Syntax (Toggle Plain Text)
  1. Point A(2,4);
  2. Point B();
  3. B = A;

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

Thanks,

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 14th, 2008
0

Re: My class is not a modifiable lvalue?

Try this.

C++ Syntax (Toggle Plain Text)
  1. class Point {
  2. public:
  3.  
  4. double x;
  5. double y;
  6.  
  7. Point() {
  8. x = 0;
  9. y = 0;
  10. }
  11.  
  12. Point(double _x, double _y);
  13. x = _x;
  14. y = _y;
  15. }
  16.  
  17. Point &operator =(const Point &p) {
  18. x = p.x;
  19. y = p.y;
  20. return *this;
  21. }
  22. };
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 14th, 2008
0

Re: My class is not a modifiable lvalue?

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).
Last edited by vijayan121; Aug 14th, 2008 at 5:08 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Aug 14th, 2008
0

Re: My class is not a modifiable lvalue?

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

Thanks!
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 15th, 2008
0

Re: My class is not a modifiable lvalue?

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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

C++ Syntax (Toggle Plain Text)
  1.  
  2. class Point{
  3. private:
  4. int x_;
  5. int y_;
  6. public:
  7. Point(){/* whatever*/}
  8. Point(int x,int y){/* whatever*/}
  9. Point(const Point& p){
  10. x_ = p.x_;
  11. y_ = p.y_;
  12. }
  13. };

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Narf!!! is offline Offline
11 posts
since Aug 2008
Aug 15th, 2008
0

Re: My class is not a modifiable lvalue?

Click to Expand / Collapse  Quote originally posted by Narf!!! ...
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)
  1.  
  2. class Point{
  3. private:
  4. int x_;
  5. int y_;
  6. public:
  7. Point(){/* whatever*/}
  8. Point(int x,int y){/* whatever*/}
  9. Point(const Point& p){
  10. x_ = p.x_;
  11. y_ = p.y_;
  12. }
  13. };

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)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Point {
  5. public:
  6.  
  7. double x;
  8. double y;
  9.  
  10. Point()
  11. {
  12. cout << "I'm in the constructor\n";
  13. x = 0;
  14. y = 0;
  15. }
  16.  
  17. Point(double _x, double _y)
  18. {
  19. x = _x;
  20. y = _y;
  21. }
  22.  
  23. Point &operator =(const Point &p)
  24. {
  25. x = p.x;
  26. y = p.y;
  27. return *this;
  28. }
  29. };
  30.  
  31.  
  32.  
  33. int main ()
  34. {
  35. Point B(); // nothing displayed
  36. Point C; // constructor call
  37. B(); // function call
  38. Point D (); // nothing displayed
  39. return 0;
  40. }
  41.  
  42.  
  43. Point B ()
  44. {
  45. cout << "I'm in the function\n";
  46. Point* A = new Point (); // constructor call
  47. return *A;
  48. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Aug 15th, 2008
0

Re: My class is not a modifiable lvalue?

Thanks a lot!!!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Narf!!! is offline Offline
11 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ 2008 - Printing Richtextbox control
Next Thread in C++ Forum Timeline: static members problem!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC