Original Post
Ok I have a few questions for all you STL gurus. I am having trouble with a templated map function when I use the [] operator. I did a little research, and it says that I should overload the < operator, but I could not get that to work at all. So after a few hours of trying to figre it out myself, I have decided to ask if anyone has any tips/ideas on what the heck I have to do to get this to work correctly. I made a super slim version of my code to only show what does not work - I deleted out a lot of stuff. The errors only happen when I use the [] operator on m_List. The error readout I get is: Second question - does anyone know why on this reference to STL map, std::map does not work correctly? If I try that way it says that it does not take 3 arguments - in my compiler it shows that it takes 4, none of which are a function address. Third Question - how the heck do I return a iterator?! I tried using:
#include <map>
template <class t>
class Test
{
public:
static std::map <int, Test<t>*> ObjectList;
public:
};
template <class T>
std::map <int, Test<T>*> Test<T>::ObjectList;
class ClassTest : public Test< ClassTest >
{
private:
public:
ClassTest()
: Test<ClassTest>()
{
}
};
std::map< std::string, ClassTest*> m_List;
int main( int argc, char** argv )
{
m_List["ClassTest 1"] = new ClassTest;
m_List["ClassTest 2"] = new ClassTest;
m_List["ClassTest 3"] = new ClassTest;
m_List["ClassTest 4"] = new ClassTest;
m_List["ClassTest 5"] = new ClassTest;
return 0;
}
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(1170) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xtree(1170) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\utility(73) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<''
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
f:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(655) : see declaration of 'std::operator`<''
std::map <int, Test<t>*>::iterator GetItr()
{
return ObjectList.begin();
}
And that does not work. It alwasy says that error C2146: syntax error : missing ';' before identifier 'GetItr'. Is there any way that you can return a specific iterator like that. I tried looking into the std::iterator class but I can't figure out the ump-teen arguments it takes between the < and >'s. Thanks for any help in advanced! I will be checking back in like 9 hours or so.