STL vectors with structs

Started by
3 comments, last by Monder 21 years, 6 months ago
Ok I''ve been playing around with STL and I tried to do this struct Test { int m_iNum; }; vector TestVector; However it gives me all these error messages whenever I compile C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: ''bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &,c onst class std::reverse_iterator<`template-parameter-1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &)'' : could not deduce template argument for ''const class std::reverse_iterator<`template-parameter- 1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &'' from ''const struct Test'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: ''bool __cdecl std::operator <(const struct std:air<`template-parameter-1'',`template-parameter-2''> &,const struct std:air<`template-parameter-1'',`template-parameter-2''> &)'' : could not deduce template argument for ''const struct std:air<`template-parameter-1'',`template-parameter-2''> &'' from ''const struct Test'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2676: binary ''<'' : ''const struct Test'' does not define this operator or a conversion to a type acceptable to the predefined operator C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: ''bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &,c onst class std::reverse_iterator<`template-parameter-1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &)'' : could not deduce template argument for ''const class std::reverse_iterator<`template-parameter- 1'',`template-parameter-2'',`template-parameter-3'',`template-parameter-4'',`template-parameter-5''> &'' from ''const struct Test'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: ''bool __cdecl std::operator <(const struct std:air<`template-parameter-1'',`template-parameter-2''> &,const struct std:air<`template-parameter-1'',`template-parameter-2''> &)'' : could not deduce template argument for ''const struct std:air<`template-parameter-1'',`template-parameter-2''> &'' from ''const struct Test'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2676: binary ''<'' : ''const struct Test'' does not define this operator or a conversion to a type acceptable to the predefined operator I don''t get them if I use a type such as int. Is there any way to hold structs in a vector. I know I can use pointers to the stucts but I want to hold the actual structs in the vector not dynamically allocate them pass a pointer and free them once I''ve done. Any idea how to do this w/o causing all those errors?
Advertisement
try this...vector<Test>TestVector
It''s looking for an operator < for your class. The cause isn''t vector--rather, you''re probably trying to use an algorithm that needs this relational operator, such as sort.

Either create a predicate for your class and pass in that to the algorithm in question, or create a < operator for your class:
bool Test::operator < (const Test &other) const
{ /* ... */ }
Well I''m only using


  struct test{   int m_iNum;};vector<test> VectorTest;  


As you can see there are no overloaded operators just an int. Oh btw when I actually use this the struct is going to have more in it than an integer it''s just I like to test things like this in a seperate little project so I can make sure it all works.

  #include <vector>using namespace std;struct test{   int m_iNum;};vector<test> VectorTest;  

--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp

test.obj - 0 error(s), 0 warning(s)

As you can see, there are no problems with this code. Your problem is elsewhere. You''re either using an algorithm or a container (such as set or map) which requires operator < defined for test.

This topic is closed to new replies.

Advertisement