Advertisement

Another vector template problem

Started by February 08, 2000 06:59 AM
2 comments, last by dirtydevil 25 years, 1 month ago
I can''t use the vector template on visual C++ in the same way I used on borlands, and I can''t figure out what is the problem, I have the following class: class CPointerInfo { public: UCHAR *pBegin, pEnd; } but when I try to create the vector with: vector vPointerInfo; I get the following errors: 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 class CPointerInfo'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: ''bool __cdecl std::operator <(const struct std::pair<`template-parameter-1'',`template-parameter-2''> &,const struct std::pair<`template-parameter-1'',`template-parameter-2''> &)'' : could not deduce template argument for ''const struct std::pair<`template-parameter-1'',`template-parameter-2''> &'' from ''const class CPointerInfo'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2676: binary ''<'' : ''const class CPointerInfo'' 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 class CPointerInfo'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: ''bool __cdecl std::operator <(const struct std::pair<`template-parameter-1'',`template-parameter-2''> &,const struct std::pair<`template-parameter-1'',`template-parameter-2''> &)'' : could not deduce template argument for ''const struct std::pair<`template-parameter-1'',`template-parameter-2''> &'' from ''const class CPointerInfo'' C:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2676: binary ''<'' : ''const class CPointerInfo'' does not define this operator or a conversion to a type acceptable to the predefined operator I tried to create the > and < operators on the class but I still get errors. AAHHHHH Thanks!
http://www.stas.net/rafael/icq: 11915640
I think you want to declare the vector like this.

vector vPointerInfo;

or

std::vector vPointerInfo;


You don''t need to define operator< for vectors unless you want to sort them or something.
Advertisement
Hey, whoever jumps into this conversation needs to be able to use the &amplt and &ampgt symbols instead of typing < and > directly: they don''t show up in HTML!!

If you''re using vector alone, there''s no need to overload operator &lt. However, if you use any kind of sorting algorithms, you do. From your error warnings, the problem is in XUTILITY line 45, which is the function template for lexicographical_compare. In order for this to work correctly, you need the overload operator < for your class to return true iff (if and only if) *this is less than the argument in your ordering scheme.

I see you tried to overload operator <, but had no success. My guess is that you didn''t do it correctly. Try this:
bool CPointerInfo::operator < (const CPointerInfo& rhs){  UCHAR *pThis = pBegin;    // point to beginning of *this array  UCHAR *pRhs = rhs.pBegin; // point to beginning of rhs array  // loop until end is reached  while (pThis != pEnd && pRhs != rhs.pEnd)  {    if (*pThis < *pRhs)      return true;    if (*pThis > *pRhs)      return false;  }  if (pThis == pEnd && pRhs != rhs.pEnd)    return true;  return false;} 


This code performs an ASCII compare on both strings, so case is not taken into account. It returns true if *this is shorter than rhs as well. It also assumes that pEnd points to the last valid UCHAR in the string. There are many ways for this code to fail, especially if CPointerInfo isn''t initialized correctly, so beware. This is just something to help you get started.
I don''t know whats the problem, I have made an overload of the less and greater operators correctly, just as you did. The error messages changed for "; exepected before operator less(to avoid html problems)". But I just finished upgrade from "visual C 5" to 6 and it accepted my syntax, without the overload... Just as borland and g++ did, so I don''t know what the problem was, but it is solved by the new visual C...

Thanks anyway
http://www.stas.net/rafael/icq: 11915640

This topic is closed to new replies.

Advertisement