Compile error using pointer-to-member function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Compile error using pointer-to-member function

  1. #1
    Join Date
    Apr 2009
    Posts
    8

    Compile error using pointer-to-member function

    When this code is compiled:

    Code:
    #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:
    Code:
        value.*method();
    What is needed to get this program to work correctly?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Try this instead:
    Code:
    (value.*method)();
    For more information, see http://www.devx.com/cplus/Article/16328/0/page/3 tip #6.
    Last edited by Danny; 05-19-2009 at 04:58 PM.
    Danny Kalev

  3. #3
    Join Date
    Apr 2009
    Posts
    8
    Yeah, I figured that out shortly after posting.
    Thanks for the input.

Similar Threads

  1. Replies: 7
    Last Post: 07-23-2008, 04:53 AM
  2. Replies: 4
    Last Post: 09-28-2007, 01:43 AM
  3. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  4. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 05:21 PM
  5. I am helpless - Edit Info form
    By Dan in forum VB Classic
    Replies: 0
    Last Post: 03-17-2000, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center