DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  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 03: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, 03:53 AM
  2. Replies: 4
    Last Post: 09-28-2007, 12:43 AM
  3. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 11:18 AM
  4. Getting a list of files into an array
    By Scott in forum VB Classic
    Replies: 12
    Last Post: 12-21-2001, 04:21 PM
  5. I am helpless - Edit Info form
    By Dan in forum VB Classic
    Replies: 0
    Last Post: 03-17-2000, 05:14 AM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links