Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2006, 11:06 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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.
codylee270 is offline   Reply With Quote
Old Apr 18th, 2006, 11:13 PM   #2
rollin92
Newbie
 
Join Date: Apr 2006
Posts: 2
Rep Power: 0 rollin92 is on a distinguished road
Don't you need Cartesian:: in front of the operator= ?

That may not be right, but I think that is the problem.
rollin92 is offline   Reply With Quote
Old Apr 18th, 2006, 11:33 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by rollin92
Don't you need Cartesian:: in front of the operator= ?

That may not be right, but I think that is the problem.

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"
Jessehk is offline   Reply With Quote
Old Apr 18th, 2006, 11:58 PM   #4
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
OMG! Yes, it would help associate the class with its member function. Jessehk, could you explain the need for the consts.
codylee270 is offline   Reply With Quote
Old Apr 19th, 2006, 12:02 AM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by codylee270
Jessehk, could you explain the need for the consts.

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!
Jessehk is offline   Reply With Quote
Old Apr 19th, 2006, 1:05 AM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
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).
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC