CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2013
    Posts
    2

    template and Visual : unable to match function definition to an existing declaration

    Hi,

    Here's a piece of code that compiled with GCC, but not with Visual 2010:

    Code:
    template < class TPRIORITY, class T >
    class cf_list :
        public std::list< std::pair<TPRIORITY, T> >
    {
        public:
            ...
    
            /// \brief Returns a reference to the element referenced by iterator _it.
            T get_value( const typename std::list< std::pair< TPRIORITY, T > >::iterator& _it );
    
            ...
    };
    
    
    template <class TPRIORITY, class T>
    T cf_list<TPRIORITY, T>::get_value(
            const typename std::list< std::pair< TPRIORITY, T > >::iterator& _it
            )
    {
        // Second element is the stored value
        return ( *_it ).second;
    }
    The error is the following:
    Code:
    cf_list.hpp(230): error C2244: 'cf_core::cf_list<TPRIORITY,T>::get_value' : unable to match function definition to an existing declaration
    ...
    see declaration of 'cf_core::cf_list<TPRIORITY,T>::get_value'
    1>          definition
    1>          'T cf_core::cf_list<TPRIORITY,T>::get_value(const std::list<std::pair<_Other1,_Other2>>::iterator &)'
    1>          existing declarations
    1>          'T cf_core::cf_list<TPRIORITY,T>::get_value(const std::_List_iterator<std::_List_val<_Ty,_Alloc>> &)'
    Thanks in advance for any ideas...

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: template and Visual : unable to match function definition to an existing declarat

    a possible workaround should be

    Code:
    template < class TPRIORITY, class T >
    class cf_list :
        public std::list< std::pair<TPRIORITY, T> >
    {
        public:
            ...
    
            using std::list< std::pair< TPRIORITY, T > >::iterator; // pull in the base member type
    
            /// \brief Returns a reference to the element referenced by iterator _it.
            T get_value( const iterator& _it );
    
            ...
    };
    
    
    template <class TPRIORITY, class T>
    T cf_list<TPRIORITY, T>::get_value(
            const iterator& _it
            )
    {
        // Second element is the stored value
        return ( *_it ).second;
    }
    this should be also preferred as it would eventually resolve (or make less error prone) other name lookup issues ( eg. bad things will happen if later on you forget to fully qualify "iterator" while another type named "iterator" is visible in the same scope ... ).

  3. #3
    Join Date
    Jul 2013
    Posts
    2

    Re: template and Visual : unable to match function definition to an existing declarat

    It works, and it is cleaner... Thanks a lot !

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: template and Visual : unable to match function definition to an existing declarat

    Quote Originally Posted by Locus06 View Post
    Code:
    template < class TPRIORITY, class T >
    class cf_list :
        public std::list< std::pair<TPRIORITY, T> >
    {
        public:
            ...
    
            /// \brief Returns a reference to the element referenced by iterator _it.
            T get_value( const typename std::list< std::pair< TPRIORITY, T > >::iterator& _it );
    
            ...
    };
    It's a bad idea to inherit publicly from std::list (or any other container from the STL). I also don't see the need for it from the code you posted. 'get_value' does not access the list; only the element through the iterator you passed, so there is no need to make this a member function. Moreover, since the list is not changed, the function should be const.
    What I really don't get, is why you would want to return a copy of the element value when an iterator (not const_iterator) is passed. You'll be spending some late-night debugging sessions when you start writing code like this:
    Code:
    cf_list<int, int> myList;
    //...
    myList.get_value(myList.begin()) = 42;
    The last line does nothing, because it only modifies the copy.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured