Compile error using pointer-to-member function

When this code is compiled:

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
#include <iostream>
using std::ostream;

class Value
{
public:
    explicit Value(int amount) : m_amount(amount) {}
    void Increment() { ++m_amount; }
    void WriteTo(ostream& stream) const { stream << m_amount; }
private:
    int m_amount;
};

ostream& operator << (ostream& stream, const Value& value)
{
    value.WriteTo(stream);
    return stream;
}

int main()
{
    typedef void (Value::*ValueMethod)(void);
    
    Value value(41);
    
    ValueMethod method = &Value::Increment;
    
    value.*method();
    
    std::cout << "The value is " << value << std::endl;

    return 0;
}

It will produce an error like this:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘method (...)’

for the code:
value.*method();
What is needed to get this program to work correctly?
It's (value.*method)();

Silly, isn't it?
Last edited on
Topic archived. No new replies allowed.