CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Posts
    3

    Compiling error is killing me -- templates and const!!

    Hi my saviors out there,

    I am new to C++ (converted from java) have been having so much trouble with const-ness and template classes. Here is a "slim" version of my code that produces the same errors (the includes and such are omitted):

    Code:
    template <	class T, 
    			class Hash = std::hash<T>,
    			class Pred = std::equal_to<T>,
    			class Alloc = std::allocator< std::pair<const T, double> > >
    class Sort
    {
    
    public:
    	typedef typename std::unordered_map<T, double, Hash, Pred, Alloc>::value_type value_type;
    	typedef typename std::unordered_map<T,double, Hash, Pred, Alloc>::const_iterator const_iterator;
    
    	std::unordered_map<T, double, Hash, Pred, Alloc> m_cMap;
    	
    	const std::vector<value_type, Alloc> sort() const;
    
    	template<class T_, class Hash_, class Pred_, class Alloc_>
    	friend std::ostream& operator<< (std::ostream& out,const Sort<T_,Hash_,Pred_,Alloc_>& counter);
    };
    
    template<class T, class Hash, class Pred, class Alloc>
    const std::vector<typename Sort<T,Hash,Pred,Alloc>::value_type, Alloc> Sort<T,Hash,Pred,Alloc>::sort() const
    {
    	std::vector<typename Sort<T,Hash,Pred,Alloc>::value_type, Alloc> results;
    	const_iterator it = m_cMap.begin();
    	while (it != m_cMap.end())
    	{
    		results.push_back(*it);
    	}
    	std::sort(results.begin(), results.end());
    	return results;
    }
    And here is a simple program using the above class:
    Code:
    #include "Sort.h"
    #include <iostream>
    #include <vector>
    
    
    int main()
    {
    	Sort<int> mySort;
    	const std::vector<typename Sort<int>::value_type> sorted = mySort.sort();
    	return 0;
    }
    And the complaint is:

    Code:
    1>  DebugTestLab.cpp
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(260): error C2166: l-value specifies const object
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(259) : while compiling class template member function 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
    1>          with
    1>          [
    1>              _Ty1=const int,
    1>              _Ty2=double
    1>          ]
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1307) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
    1>          with
    1>          [
    1>              _Ty1=const int,
    1>              _Ty2=double
    1>          ]
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1301) : while compiling class template member function 'void std::vector<_Ty,_Ax>::_Tidy(void)'
    1>          with
    1>          [
    1>              _Ty=std::pair<const int,double>,
    1>              _Ax=std::allocator<std::pair<const int,double>>
    1>          ]
    1>          c:\users\xiaoye\documents\visual studio 2010\projects\testlab\testlab\debugtestlab.cpp(12) : see reference to class template instantiation 'std::vector<_Ty,_Ax>' being compiled
    1>          with
    1>          [
    1>              _Ty=std::pair<const int,double>,
    1>              _Ax=std::allocator<std::pair<const int,double>>
    1>          ]
    1>
    1>Build FAILED.
    Please help me, it has been repeatedly driving me nuts for the past week! I feel like my balls are being kicked at 1Hz for hours.

  2. #2
    Join Date
    Jun 2012
    Posts
    3

    Re: Compiling error is killing me -- templates and const!!

    Please ignore the friend operator<< in the class declaration -- I forgot to take it out for the "slim" code. thanks!

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Compiling error is killing me -- templates and const!!

    Code:
    #include <iostream>
    #include <vector>
    #include <functional>       // for std::hash
    #include <memory>           // for std::allocator
    #include <unordered_map>    // for std::unordered_map	
    #include <algorithm>        // for std::sort
    
    template <	class T, 
    			class ValueType  = std::pair<T, double>,
    			class Hash       = std::hash<T>,
    			class Pred       = std::equal_to<T>,
    			class Alloc      = std::allocator<ValueType> >
    class Sort
    {
    	typedef typename std::unordered_map<T, double, Hash, Pred, Alloc>::const_iterator const_iterator;
    
    public:
    	typedef ValueType value_type;
    	typedef Alloc     value_alloc;
    
    	std::unordered_map<T, double, Hash, Pred, Alloc> m_cMap;
    	
    	const std::vector<value_type, value_alloc> sort() const
    	{
    		std::vector<value_type, value_alloc> results;
    		const_iterator it = m_cMap.begin();
    		while (it != m_cMap.end())
    		{
    			results.push_back(*it);
    			it++;
    		}
    		std::sort(results.begin(), results.end());
    		return results;
    	}
    };
    
    
    
    int main()
    {
    	Sort<int> mySort;
    	const std::vector<typename Sort<int>::value_type> sorted = mySort.sort();
    	return 0;
    }
    Best regards,
    Igor

  4. #4
    Join Date
    Jun 2012
    Posts
    3

    Re: Compiling error is killing me -- templates and const!!

    Quote Originally Posted by Igor Vartanov View Post
    Code:
    #include <iostream>
    #include <vector>
    #include <functional>       // for std::hash
    #include <memory>           // for std::allocator
    #include <unordered_map>    // for std::unordered_map	
    #include <algorithm>        // for std::sort
    
    template <	class T, 
    			class ValueType  = std::pair<T, double>,
    			class Hash       = std::hash<T>,
    			class Pred       = std::equal_to<T>,
    			class Alloc      = std::allocator<ValueType> >
    class Sort
    {
    	typedef typename std::unordered_map<T, double, Hash, Pred, Alloc>::const_iterator const_iterator;
    
    public:
    	typedef ValueType value_type;
    	typedef Alloc     value_alloc;
    
    	std::unordered_map<T, double, Hash, Pred, Alloc> m_cMap;
    	
    	const std::vector<value_type, value_alloc> sort() const
    	{
    		std::vector<value_type, value_alloc> results;
    		const_iterator it = m_cMap.begin();
    		while (it != m_cMap.end())
    		{
    			results.push_back(*it);
    			it++;
    		}
    		std::sort(results.begin(), results.end());
    		return results;
    	}
    };
    
    
    
    int main()
    {
    	Sort<int> mySort;
    	const std::vector<typename Sort<int>::value_type> sorted = mySort.sort();
    	return 0;
    }
    So, basically, vector cannot take
    Code:
    std::pair<const T, double>
    as its type. Can you explain to me why it is the case? Is it just the way it is supposed to be?
    Last edited by xynlp; June 6th, 2012 at 05:14 PM. Reason: typo

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Compiling error is killing me -- templates and const!!

    1> DebugTestLab.cpp
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(260): error C2166: l-value specifies const object

    Code:
    // C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility
    // line 260
        pair& operator=(pair<_Ty1, _Ty2>&& _Right)
            {   // assign from moved pair
            this->first = _STD move(_Right.first);
            this->second = _STD move(_Right.second);
            return (*this);
            }
    To be assigned std:: pair this->first cannot be of a const type because of the code above. And std::vector:: push_back needs to make a copy of pushed object.
    Last edited by Igor Vartanov; June 6th, 2012 at 05:37 PM.
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Compiling error is killing me -- templates and const!!

    Quote Originally Posted by xynlp View Post
    So, basically, vector cannot take
    Code:
    std::pair<const T, double>
    as its type. Can you explain to me why it is the case? Is it just the way it is supposed to be?
    The problem is not just vector, it is any class or code where you are attempting to make copies of this type.
    Code:
    #include <map>
    
    typedef std::pair<const int, double> SomeType;
    int main()
    {
        SomeType a;
        SomeType b = a;
        SomeType c;
        c = a;
    }
    Does this simple code produce a similar error? If it does, then any construct that copy constructs/assigns a SomeType will fail to compile, and vector's internals does these very operations.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 6th, 2012 at 09:49 PM.

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

    Re: Compiling error is killing me -- templates and const!!

    Quote Originally Posted by xynlp View Post
    Here is a "slim" version of my code that produces the same errors (the includes and such are omitted):
    I sure hope you only wrote this as an example to clarify your problem, because all of that code can be reduced to just three lines.
    Code:
    std::unordered_map<int, double> cMap;
    std::vector<std::pair<int, double>> sorted(cMap.begin(), cMap.end());
    std::sort(sorted.begin(), sorted.end());
    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