![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
simple program and = operator overload errors
The program is straight forward. You enter a x and y value, it tells you what it is, and then it copies it.
#include <iostream>
#include <iomanip>
using namespace std;
class Cartesian
{
private:
double x;
double y;
public:
Cartesian(double = 0, double = 0);
void setXY( double , double);
double showX(){return x;};
double showY(){return y;};
void operator=(Cartesian &);
};
Cartesian::Cartesian( double in_x, double in_y)
{
x = in_x;
y = in_y;
}
void operator=(Cartesian& newXY)
{
x = newXY.x;
y = newXY.y;
}
int main()
{
double x,y;
cout << "\nPlease input a X coordinate: ";
cin >> x;
cout << "\nPlease input a Y coordinate: ";
cin >> y;
Cartesian xy1(x, y), xy2;
cout << "The first coordinate pair is - " << xy1.showX() << "," << xy1.showY() << endl;
xy2 = xy1;
cout << "The second coordinate pair is - " << xy2.showX() << "," << xy2.showY() << endl;
system("PAUSE");
return 0;
}errors: must be a nonstatic member function `void operator=(Cartesian&)' must take exactly two arguments In function `void operator=(Cartesian&)': `x' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) `double Cartesian::x' is private within this context `y' undeclared (first use this function) `double Cartesian::y' is private within this context These are probably easy, but my class-writing is rusty and I typically don't understand what to do when the compiler yells at me. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Apr 2006
Posts: 2
Rep Power: 0
![]() |
Don't you need Cartesian:: in front of the operator= ?
That may not be right, but I think that is the problem. |
|
|
|
|
|
#3 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
Yup, I think that's it. Also, try using const correctness. Those getters should be defined like this: double showX() const {return x;};
double showY() const {return y;};![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! Last edited by Jessehk; Apr 18th, 2006 at 11:46 PM. Reason: Replaced "declared" with "defined" |
|
|
|
|
|
|
#4 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
OMG! Yes, it would help associate the class with its member function. Jessehk, could you explain the need for the consts.
|
|
|
|
|
|
#5 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Quote:
I'll let somebody who knows what they're talking about with reasonable certainty do that :p.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
The consts at the end of the function simply tell the compiler that the function does not change the value of the object, so its legal to call the function on a const instance of that class. This becomes really useful when you pass around constant references to an object and want to read one of its properties (i.e. size of a const string).
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|