Error with pointers to member variables as parameters; why?
-
25-06-2021 - |
Domanda
Why do I keep on getting the following error in this code in Visual C++ 2010, and how do I fix it while maintaining the type inference capability for the member variable?
error C2825:
'Foo<T>::value_type':
must be a class or namespace when followed by'::'
template<class T>
struct Foo
{
typedef typename T::value_type value_type;
template<class M>
void foo(M value_type::*member) const; // error
};
struct S { typedef int value_type; };
int main() { Foo<S> s; }
Soluzione
The template parameter T
turns out to be type S
, therefore value_type
turns out to be int
(the nested-type in S
). So how can you write value_type::*member
? Note that it turns out to be int::*member
which doesn't make sense. int
is not a class type.
I think you meant T::*member
instead of value_type::*member
.
Altri suggerimenti
value_type is not a member of structure S. Its just a typedef so you cant access it as you are doing.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow