My class is not a modifiable lvalue?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

My class is not a modifiable lvalue?

 
0
  #1
Aug 14th, 2008
I have a very simple class

  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:

  1. Point A(2,4);
  2. Point B();
  3. B = A;

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

Thanks,

Dave
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,431
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: My class is not a modifiable lvalue?

 
0
  #2
Aug 14th, 2008
Try this.

  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. };
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: My class is not a modifiable lvalue?

 
0
  #3
Aug 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: My class is not a modifiable lvalue?

 
0
  #4
Aug 14th, 2008
ahhh that was the key ! Needed to use B instead of B()

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 11
Reputation: Narf!!! is an unknown quantity at this point 
Solved Threads: 1
Narf!!! Narf!!! is offline Offline
Newbie Poster

Re: My class is not a modifiable lvalue?

 
0
  #5
Aug 15th, 2008
Originally Posted by vijayan121 View Post
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

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: My class is not a modifiable lvalue?

 
0
  #6
Aug 15th, 2008
Originally Posted by Narf!!! View Post
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

  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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 11
Reputation: Narf!!! is an unknown quantity at this point 
Solved Threads: 1
Narf!!! Narf!!! is offline Offline
Newbie Poster

Re: My class is not a modifiable lvalue?

 
0
  #7
Aug 15th, 2008
Thanks a lot!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC