typedef cause no global operator found error

hello,
here is the stuation

1
2
3
4
5
6
7
typedef vmml::Vector3<float> vec3

vec3 a;
vec3 b;

a += b; /* error C2677: binary '+=' : no global operator found which takes type 'amz::vec3' (or there is no acceptable conversion) */


altough I read a topic same to this stuation, which suggest not only put a class into a namespace but also its operators too, but this is not the case I suppose because all operators already declared in class, and I don't know what to do ? any idea would be nice ?
I hate to point out the obvious... but is the += operator overloaded for your Vector3 class?
sure there is one += operator for vmml::Vector3<T> but when I typedef it, compiler just doesn't recognize it

1
2
3
4
5
6
7
8
9
template < typename T > 
const Vector3< T >& Vector3< T >::operator+=( T  a ) 
{ 
    x += a; 
    y += a; 
    z += a; 
    return *this; 
} 
Ah.

No, see, the typedef has nothing to do with it.

Your += operator takes a T as the right-hand value. This is different from a Vector3<T>

1
2
3
4
5
6
7
8
typedef vmml::Vector3<float> vec3;

vec3 a;
vec3 b;
float c;

a += c; // works just fine
a += b; // fails because += doesn't take a Vector3 on the right 
closed account (1yR4jE8b)
Try this:

1
2
3
4
5
6
7
8
9
template < typename T > 
const Vector3< T >& Vector3< T >::operator+=(const Vector3&  a ) 
{ 
    x += a.x; 
    y += a.y; 
    z += a.z; 
    return *this; 
} 
Or better yet:

1
2
3
4
5
6
7
8
template < typename T > 
Vector3< T >& Vector3< T >::operator+=(const Vector3<T>&  a ) 
{ 
    x += a.x; 
    y += a.y; 
    z += a.z; 
    return *this; 
}


well strange but when I test them in a diffrent scope to clerify they works in vmml::Vector3 format, I also test if they works in short format to which is vec3 they both worked just fine wierd...

1
2
3
4
5
6
7
8
9
10
11
12
13
		vmml::Vector3<float> a(3,3,3);
		vmml::Vector3<float> b(2,2,2);

		a += b;

		cout<<a<<endl;

		vec3 x(3,3,3);
		vec3 y(2,2,2);

		x += y;

		cout<<x<<endl;


this vec3 typedef just doesn't work in the header file, I may miss to include some header. I'll have a look at the header file but thanks for the replie which gives me a new stage to investigate
There is no reason typedef would make any difference here, that I can see.

Can you post the Vector3 class in full? If it's too large to post on the forums, maybe put it on on pastebin or something.

I have been working whit this library for a month it is well written, I gave the wrong overloaded version for the above post, there is an apropriate version of the operator

1
2
3
4
5
6
7
8
template < typename T > 
const Vector3< T >& Vector3< T >::operator+=( const Vector3& rhs ) 
{ 
    x += rhs.x; 
    y += rhs.y; 
    z += rhs.z; 
    return *this; 
} 


there is the header file which cause error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	// Vector3 Proxy class
	class PVec3 {
	public:
		vec3* mVec;
		
		static const char className[];
		static Luna<PVec3>::RegType methods[];

		PVec3(lua_State* L){
			// get number of arguments
			int n = lua_gettop(L);

			// check if we have 3 arguments 
			if(n != 3)
				luaL_error(L, "Got %d arguments expected 3 (x, y, z)", n);
			
			// get arguments
			double x = luaL_checknumber(L, 1);
			double y = luaL_checknumber(L, 2);
			double z = luaL_checknumber(L, 3);

			mVec = new vec3(x, y, z);
		}


		int getx(lua_State *L) {
			lua_pushnumber(L, mVec->x);
			return 1;
		}

		int gety(lua_State *L) {
			lua_pushnumber(L, mVec->y);
			return 1;
		}

		int getz(lua_State *L) {
			lua_pushnumber(L, mVec->z);
			return 1;
		}

		int length(lua_State *L) {
			lua_pushnumber(L, mVec->length());
			printf("length = %f\n", mVec->length());
			return 1;
		}

		int addv(lua_State *L) {
			PVec3 *b = Luna<PVec3>::check(L, 1);
			//mVec += *b->mVec;
			printf("new x = %f y = %f z = %f\n", mVec->x, mVec->y, mVec->z);
			return 0;
		}

		~PVec3() {
			delete mVec;
			printf("deleted Vector3 (%p)\n", this);
		}

	};


and there is the other header that I made typedefs

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

#define amzMathType float

#include <vmmlib/vector3.h>
#include <vmmlib/vector4.h>
#include <vmmlib/quaternion.h>
#include <vmmlib/matrix3.h>
#include <vmmlib/matrix4.h>

namespace amz {

	// vmml imports
	typedef vmml::Vector3<amzMathType> vec3;
	typedef vmml::Vector4<amzMathType> vec4;
	typedef vmml::Quaternion<amzMathType> quat;
	typedef vmml::Matrix3<amzMathType> mat3;
	typedef vmml::Matrix4<amzMathType> mat4;

	// math defs
	typedef vec3 Color;

}


I realy appriciate for the replies, finaly I found the mistake I was trying to use a pointer at left hand of the = operator, which doesn't have the apropriate overloaded function

1
2
mVec += *b->mVec // left side of the operator is a pointer !
*mVec += *b->mVec // works just fine  
closed account (1yR4jE8b)
Or better yet:
1
2
3
4
5
6
7
8
template < typename T > 
Vector3< T >& Vector3< T >::operator+=(const Vector3<T>&  a ) 
{ 
    x += a.x; 
    y += a.y; 
    z += a.z; 
    return *this; 
}



I don't think it matters, doesn't it compile to the same thing?
Topic archived. No new replies allowed.