CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Threaded View

  1. #1
    Join Date
    Jan 2009
    Posts
    56

    Operator Overloading, in General

    I'm having trouble overriding the + and - operators.


    My struct definition with overloaded methods:
    Code:
    struct vectorf
    {
    	float x;
    	float y;
    	float z;
    
    	const vectorf operator+(const vectorf other);
    	const vectorf operator-(const vectorf other);
    };
    
    const vectorf vectorf::operator+(vectorf other)
    {
    	vectorf ans;
    
    	ans.x = x + other.x;
    	ans.y = y + other.y;
    	ans.z = z + other.z;
    
    	return ans;
    }
    
    const vectorf vectorf::operator-(const vectorf other)
    {
    	vectorf ans;
    
    	ans.x = x - other.x;
    	ans.y = y - other.y;
    	ans.z = z - other.z;
    
    	return ans;
    }


    And this is the line of code giving me trouble:
    Code:
    glTranslateVector(MAIN_POSITION - BACKGROUND_POSITION);


    The error says:
    " error C2678: binary '-' : no operator found which takes a left-hand operand of type 'const vectorf' (or there is no acceptable conversion) "

    And:
    " could be 'const vectorf vectorf:: operator -(const vectorf)' "


    Any help would be greatly appreciated.
    -Salbris
    Last edited by Salbrismind; April 29th, 2009 at 05:01 PM. Reason: Removing unnessary sentence

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured