0

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; }
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    What's the parameter for `foo` supposed to be (in words)? – CB Bailey Aug 09 '12 at 05:59
  • @CharlesBailey: lol oops... either I made a stupid mistake in the original code, or I reduced the test case wrong... let me find the problem and I'll post an update, thanks for pointing it out. – user541686 Aug 09 '12 at 06:01
  • Please elaborate more what you are trying to achieve – Roman Saveljev Aug 09 '12 at 06:01
  • 1
    Sorry everyone -- I misinterpreted what was happening. See [my comment](http://stackoverflow.com/questions/11877464/error-with-pointers-to-member-variables-as-parameters-why#comment15804094_11877528) on Nawaz's answer. – user541686 Aug 09 '12 at 06:06

2 Answers2

3

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Oh fudge, either I made a stupid mistake in the original code, or I reduced the test case incorrectly... let me fix it; +1 thanks... – user541686 Aug 09 '12 at 05:59
  • 1
    Ohhhhhhh I figured it out -- this was a red herring. The actual problem was that I had a member function (with this weird parameter) and its template instantiation was eager instead of lazy (not sure if it's a bug or a feature, since the container was also a template...), so when I was chaining a couple of method calls, I thought the error was due to the second one, not the first one! The first one definitely had `value_type = int`, but the second one had `value_type = pair`. That makes a lot of sense, thanks! – user541686 Aug 09 '12 at 06:06
0

value_type is not a member of structure S. Its just a typedef so you cant access it as you are doing.

Yogesh
  • 565
  • 3
  • 21