visual studio intellisense error
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开发者_C百科 mean I guess it's 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?
I am using visual studio 2010. I wonder if this happens in other versions as well?
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.
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).
精彩评论