Problem with unary minus overloading

This is a discussion on Problem with unary minus overloading within the C++ Programming forums, part of the General Programming Boards category; the assignment is to make a class to handle 3 dimensional vectors and to overload all the operators associated with ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    Problem with unary minus overloading

    the assignment is to make a class to handle 3 dimensional vectors and to overload all the operators associated with them. I have it running perfectly except for unary minus. maybe you guys can help:

    prototype:

    Code:
     class Vector
      {
        public:
          Vector operator - (const Vector& v1);
          //there are about ten other functions, but they're all working
        private:
          double x;
          double y;
          double z;
         //variables to hold user inputted coordinates
       };
    
       int main()
       {
         //menu driven with 7 options, all of which work except unary 
    minus
       }
    
       //here's the definition
       Vector Vector::operator - (const Vector& v1)
       {
         Vector temp;
         
         temp.x = -v1.x;
         temp.y = -v1.y;
         temp.z = -v1.z;
        
         return temp;
      }
    it's on three files: header, application, and implementation. everything is included fine and it works when I take out the unary minus function. the error message I get says it can't find a function to match my use of unary minus and I can't figure it out
    Last edited by Strait; 01-14-2005 at 01:55 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    Code:
       //here's the definition
       Vector Vector::operator - (const Vector& v1)
       [
         Vector temp;
         
         temp.x = -v1.x;
         temp.y = -v1.y;
         temp.z = -v1.z;
        
         return temp;
      }
    That is supposed to be a brace ( { ), not a bracket ( [ ).

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    sorry, typo

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The unary minus operator doesn't take a parameter. It should look like this:
    Code:
    Vector Vector::operator-()
    {
         Vector temp;
         
         temp.x = -x;
         temp.y = -y;
         temp.z = -z;
        
         return temp;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    HA!

    good call, that's what I was originally going to do, but my book told me otherwise

    of course, now I've got another problem. for some reason I keep getting a fatal symbol referenceing error. I think it's associated with my output function

    Code:
     
    //prototype in same class as my last problem
    
    friend ostream& operator << (ostream& outs, const Vector& v1);
    
    //definition
    
    ostream& operator << (ostream& outs, const Vector& v1)
    {
      outs<<"(" << v1.get_x() <<","<< v1.get_y() <<","<< v1.get_z() <<")";
      return outs;
    }
    is there anything wrong there? if you can think of anything I could try I'd appreciate it

    oh, and the output is supposed to look like this: (x,y,z)
    Last edited by Strait; 01-14-2005 at 02:20 PM.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I don't get that problem:
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::ostream;
    
    class Vector
    {
        public:
    		double get_x() const {return x;}
    		double get_y() const {return y;}
    		double get_z() const {return z;}	
          //there are about ten other functions, but they're all working
        public:
          double x;
          double y;
          double z;
         //variables to hold user inputted coordinates
    
    	friend ostream& operator << (ostream& outs, const Vector& v1);
    };
    
    
    ostream& operator << (ostream& outs, const Vector& v1)
    {
    	outs<<"(" << v1.get_x() <<","<< v1.get_y() <<","<< v1.get_z() <<")";
    	return outs;
    }
    
    int main()
    {
    	Vector a;
    	a.x = 2;
    	a.y = 4;
    	a.z = 6;
    	cout << a << endl;
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    yeah, I just tried it with a new main and it ran fine, so the problem must be in my application file. thank god, that makes things a lot easier

    thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21