Thread: Operator overloading compilation issue in GCC

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    21

    Operator overloading compilation issue in GCC

    Just porting a small project of mine from Windows to Mac and I've hit a compile issue I'm a bit confused about.

    I used CMake to build an XCode project and when I try and compile my maths library (of which I'm using operator overloading in my vector class) I'm getting errors such as this output for all the overloaded operators:

    Code:
    AmVector2.h:22: error: extra qualification 'AmberMaths::AmVector2::' on member 'operator+'
    Here's the declaration and implementation of the above function:

    Code:
    AmVector2 AmVector2::operator+(AmVector2& rVec);
    
    AmVector2 AmVector2::operator+(AmberMaths::AmVector2 &rVec)
    {
    	AmVector2 vec;
    	vec.Set(x + rVec.x, y + rVec.y);
    
    	return vec;
    }
    It compiled fine in MSVC's compiler but GCC obviously isn't happy with something.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Post more code, why the definition contains scoped parameter? How can we know which line is 22?


    EDIT:
    Code:
    AmVector2 AmVector2::operator+(AmVector2& rVec);
    Try:

    Code:
    AmVector2 operator+(AmVector2& rVec);

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    21
    I've removed the top seven lines from both files as that contains identifying information

    AmVector2.h:
    Code:
    #ifndef _AM_VECTOR_2_H
    #define _AM_VECTOR_2_H
    
    namespace AmberMaths
    {
    	class AmVector2
    	{
    	public:
    		AmVector2();
    		AmVector2(float x, float y);
    		~AmVector2();
    
    		void Set(float x, float y);
    
    		AmVector2 AmVector2::operator+(AmVector2& rVec);
    		AmVector2 AmVector2::operator-(AmVector2& rVec);
    		void AmVector2::operator+=(AmVector2& rVec);
    		void AmVector2::operator-=(AmVector2& rVec);
    		AmVector2 AmVector2::operator*(float fVal);
    		void AmVector2::operator*=(float fVal);
    
    		void Normalise();
    
    		float GetLength();
    
    		float GetX() { return x; }
    		float GetY() { return y; }
    
    	private:
    		float x;
    		float y;
    
    	};
    }
    
    #endif	//_AM_VECTOR_2_H
    AmVector2.cpp:
    Code:
    #include "AmberMaths.h"
    #include "AmVector2.h"
    
    using namespace AmberMaths;
    
    AmVector2::AmVector2()
    {
    	Set(0.0f, 0.0f);
    }
    
    AmVector2::AmVector2(float x, float y)
    {
    	Set(x, y);
    }
    
    AmVector2::~AmVector2()
    {
    }
    
    void AmVector2::Set(float x, float y)
    {
    	this->x = x;
    	this->y = y;
    }
    
    AmVector2 AmVector2::operator+(AmberMaths::AmVector2 &rVec)
    {
    	AmVector2 vec;
    	vec.Set(x + rVec.x, y + rVec.y);
    
    	return vec;
    }
    
    AmVector2 AmVector2::operator-(AmberMaths::AmVector2 &rVec)
    {
    	AmVector2 vec;
    	vec.Set(x - rVec.x, y - rVec.y);
    
    	return vec;
    }
    
    void AmVector2::operator+=(AmberMaths::AmVector2 &rVec)
    {
    	Set(x + rVec.x, y + rVec.y);
    }
    
    void AmVector2::operator-=(AmberMaths::AmVector2 &rVec)
    {
    	Set(x - rVec.x, y - rVec.y);
    }
    
    AmVector2 AmVector2::operator*(float fVal)
    {
    	AmVector2 vec;
    	vec.Set(x * fVal, y * fVal);
    
    	return vec;
    }
    
    void AmVector2::operator*=(float fVal)
    {
    	Set(x * fVal, y * fVal);
    }
    
    void AmVector2::Normalise()
    {
    	float fLength = GetLength();
    	x /= fLength;
    	y /= fLength;
    }
    
    float AmVector2::GetLength()
    {
    	return sqrt((x*x) + (y*y));
    }
    Quote Originally Posted by kmdv View Post
    Try:

    Code:
    AmVector2 operator+(AmVector2& rVec);
    Actually, yeah. Makes sense. There was absolutely no requirement to have AmVector2:: on any of those. I wish I knew why I put that there. This is code I wrote years ago which I'm attempting to bring up to date

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You should pass parameters by const-reference (if you do not change them ofc):
    Code:
    AmVector2 AmVector2::operator+(const AmVector2& rVec);

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    21
    Quote Originally Posted by kmdv View Post
    You should pass parameters by const-reference (if you do not change them ofc):
    Code:
    AmVector2 AmVector2::operator+(const AmVector2& rVec);
    Yeah, I know. That's one of the changes I'm wanting to implement. I don't think there's a single instance of the word "const" in this entire code. I was a naive young coder once

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In the class definition, you don't need to prefix the function name (in this case operator+) with the name of the function. See kmdv's first post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and Cpp GCC Compiler Issues
    By Bangonkali in forum C Programming
    Replies: 15
    Last Post: 12-12-2010, 11:01 PM
  2. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  3. gcc configuration under linux advice needed
    By vart in forum Tech Board
    Replies: 9
    Last Post: 01-10-2007, 02:46 PM
  4. Inconsistent Compilation
    By GlassEyeSlim in forum Linux Programming
    Replies: 2
    Last Post: 02-23-2006, 06:43 PM
  5. MSVC++ vs GCC compiler issue
    By WDT in forum C++ Programming
    Replies: 9
    Last Post: 01-04-2004, 01:07 PM