views:

153

answers:

2
template <typename T>
class Test {
     friend Test<T> & operator * (T lhs, const Test<T> & rhs) {
      Test<T> r(rhs);
//        return r *= lhs;
     }
}

4 IntelliSense: identifier "T" is undefined

why is T defined on line 3 but not line 4? i mean i guess its not a real error just an intellisense error... it works anyway but is there something wrong? can i fix it? or remove the red squiggles somehow?

thanks

i am using visual studio 2010... i wonder if this happens in other versions as well?

A: 

Intellisense shows T as undefined because it is a generic template type. Depending on how you instantiate the class, T will be a different type. For example if you have Test<int> A, T is of type int, but if you call Test<string> A, T is of type string for that class and it's methods.

Inverse
ok but what you said applies to line 3 as well.. yet i get no error therethis only happens in friend
rakkarage
i think it's because of friend, it doesn't use any private members it's just friend to group it with rest of the operator overloads... but if i make it seperate from the class i don't get the 'error' template <typename T> class Test { } template <typename T> Vector3<T> return r *= lhs; }does it matter? better to group it?
rakkarage
A: 

It seems to be getting confused by having the definition inside the class. So changing your code to:

template <typename T>
class Test {

    friend Test<T> & operator * (T lhs, const Test<T> & rhs);
};

template <typename T>
Test<T> & operator * (T lhs, const Test<T> & rhs) {
    Test<T> r(rhs);
}

makes the problem go away.

This appears to be a bug in the compiler your code should be legal based on my reading of the spec (specifically 11.4/5).

R Samuel Klatchko