Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 29th, 2005, 8:19 AM   #1
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Operator Overloading issue [solved]

Hey all, trying to do some operator overloading on a Vector class (school, you know how it goes). And for some reason my operator= method won't actually assign the new value. Let me print some related code.
//definition
class Vector {
   float x,y,z;
   public:
      Vector();
      Vector(float, float, float);
      Vector(const Vector&);

      Vector operator + (Vector);
      Vector operator - (Vector);
      Vector operator * (Vector);
      Vector operator / (Vector);
      Vector operator = (Vector);
      Vector operator == (Vector);
      Vector operator != (Vector);
      Vector operator () (Vector);

      float dot_product(Vector);
      Vector cross_product(Vector);
      float length();
      float normalize();
      void print_vector();
};

//two constructors used
Vector::Vector(const Vector &v)
{
   x = v.x; 
   y = v.y; 
   z = v.z;
}

Vector::Vector(float new_x, float new_y, float new_z)
{
   x = new_x; 
   y = new_y; 
   z = new_z;
}

//the operator= method
Vector Vector::operator= (Vector v)
{
   Vector t (v.x, v.y, v.z);
   t.print_vector();printf("\n");
   return (t);
}

//main i've been using
int main()
{
   Vector v (1, 2, 3);
   Vector z (1, 2, 3);
   Vector q;
   v.print_vector(); printf("\n");
   z.print_vector(); printf("\n");
   q = z + v;
   q.print_vector(); printf("\n");
}

//the output
jule@dolphy ~/classes/cs265/labs/lab1 $ ./vector 
(1.000, 2.000, 3.000) //created vector v
(1.000, 2.000, 3.000) //created vector z
(2.000, 4.000, 6.000) //in operator+ method
(2.000, 4.000, 6.000) //in operator= method
(0.000, 0.000, 0.000) //q = v + z;
I hope it's nothing stupid, i might solve it before anyone else, but i post anyway in case I don't.

Thanks in advance

Dizz
__________________
naked pictures of you | PFO F@H stats

Last edited by Dizzutch; Jan 29th, 2005 at 8:46 AM.
Dizzutch is offline   Reply With Quote
Old Jan 29th, 2005, 8:37 AM   #2
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
Try this instead;
Vector& Vector::operator= (const Vector& v)
{
  x = v.x;
  y = v.y;
  z = v.z;

  return *this;
}
Don't forget to change your declaration to match.
Tama is offline   Reply With Quote
Old Jan 29th, 2005, 8:41 AM   #3
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
Ah, there we go, can you explain that real quick? I'm not sure how this method is different than mine.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jan 29th, 2005, 8:44 AM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
actually the signature Vector Vector::operator = (Vector v) works fine. no need for the &.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jan 29th, 2005, 8:46 AM   #5
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
nm, i think i see what you're doing. thanks!
__________________
naked pictures of you | PFO F@H stats
Dizzutch 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 4:40 PM.

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