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

Hybrid View

  1. #1
    Join Date
    Jul 2008
    Posts
    8

    Operator overload not defined error when type is accessed through const struct

    I have a basic vector/point class where I've overloaded a bunch of arithmetical operators:

    Code:
    #pragma once
    
    class point
    {
    public:
    
    	point() 
    	{
    
    	}
    
    	point(float p_x, float p_y, float p_z) : x(p_x), y(p_y), z(p_z) 
    	{
    
    	}
    
    	point& operator+(point& right)
    	{
    		return point(x + right.x, y + right.y, z + right.z);
    	}
    
    	point& operator-(point& right)
    	{
    		return point(x - right.x, y - right.y, z - right.z);
    	}
    
    	point& operator*(point& right)
    	{
    		return point(x * right.x, y * right.y, z * right.z);
    	}
    
    	point& operator*(const float right)
    	{
    		return point(x * right, y * right, z * right);
    	}
    
    	point& operator*(const double right)
    	{
    		return point(x * right, y * right, z * right);
    	}
    
    	point& operator+=(point& right)
    	{
    		return point(x + right.x, y + right.y, z + right.z);
    	}
    
    	point& operator*=(const point& right)
    	{
    		return point(x * right.x, y * right.y, z * right.z);
    	}
    
    	point& operator*=(point right)
    	{
    		return point(x * right.x, y * right.y, z * right.z);
    	}
    
    	point& operator*=(const float& right)
    	{
    		return point(x * right, y * right, z * right);
    	}
    
    	point& operator*=(const double& right)
    	{
    		return point(x * right, y * right, z * right);
    	}
    
    	float x;
    	float y;
    	float z;
    };
    I can use it fine like
    Code:
    point p(50,50,50);
    point q(50,50,50);
    point t = p * q + q;
    However when I put this point type into a struct and try to access the members after passing it through by const reference:
    Code:
    struct sextic {
    	point a,b,c,d,e,f,g;
    };
    
    inline static sextic sexticDifference(const sextic &p_sextic1, const sextic &p_sextic2) 
    {
    	sextic result;
    	result.a = p_sextic2.a - p_sextic1.a;
    	result.b = p_sextic2.b - p_sextic1.b;
    	result.c = p_sextic2.c - p_sextic1.c;
    	result.d = p_sextic2.d - p_sextic1.d;
    	result.e = p_sextic2.e - p_sextic1.e;
    	result.f = p_sextic2.f - p_sextic1.f;
    	result.g = p_sextic2.g - p_sextic1.g;
    
    	return result;
    }
    This gives me an "operator not defined" error for compilation. How do I fix this, please?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Operator overload not defined error when type is accessed through const struct

    That is because the member function was not declared const. If you want to overload operator+ as a member, then I would expect:
    Code:
    point operator+(const point& right) const
    {
        return point(x + right.x, y + right.y, z + right.z);
    }
    Notice the return type is point, not reference to pointer. Notice also the const reference parameter and the const at the end that declares the member function to be const.

    Another way is to overload operator+= as a member:
    Code:
    point& operator+=(const point& right)
    {
        x += right.x;
        y += right.y;
        z += right.z;
        return *this;
    }
    then define operator+ as a non-member function in terms of operator+=, e.g.,
    Code:
    point operator+(point left, const point& right)
    {
        return left += right;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator overload not defined error when type is accessed through const struct

    Quote Originally Posted by Tanaka View Post
    This gives me an "operator not defined" error for compilation. How do I fix this, please?
    As laserlight pointed out, your return of a reference is wrong here. Even though you don't get a compile error, returning references to local objects is undefined behaviour. For example:
    Code:
    point& operator+(point& right)
    {
       return point(x + right.x, y + right.y, z + right.z);
    }
    You are creating a local "point" object. Then you're returning a reference to this local object. When the operator+ function returns, that local point object is now gone. You now have a reference to something that no longer exists. You now use it in your program, and who knows what will happen -- maybe your program will "work", maybe it will crash, who knows.

    The solution as pointed out is to return a point object, not a reference.

    Regards,

    Paul McKenzie

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