Sign in to follow this  
Rhalin

Problem sending STL Container/Adapter to Template Function

Recommended Posts

Rhalin    246
I've got a strange problem that I've spent way too long looking at myself, and would appreciante any guidence anyone can give me. I'm trying to send a priority_queue to a templated function, and my compilers are giving me all kinds of nastyness that I'm having trouble sifting through. The function works fine for all basic container types that support the two functions I'm using (top() and pop()) and some that dont support it, that I've added some wrappers for. But anytime I try to send one of the STL comparison functions (as a sorting predicate), the compilers start spitting out all kinds of junk to me. Here's a (much much stripped down) example that replicates the problem I'm coming acrossed
//Disable Visual Studio warning with STL containers that create
// debug names longer than 255 characters.
//That is, any STL item containing another STL item
//This makes the compile look a lot cleaner for vector<stringstream>
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <fstream>
#include <list>
#include <vector>
#include <queue>

using namespace std;

template<class T>
void printResults(T & container);

typedef priority_queue<string,list<string>,greater<string> > pqgType;

int main(int argc, char ** argv){

  pqgType pqgResults;
  pqgResults.push("test1");
  pqgResults.push("test2");
  pqgResults.push("test3");

  printResults<pqgType>(pqgResults);
  cout << endl;
  return 0;
}
/*
  print contents of container, one item at a time
*/

template<class T> inline
void printResults(T & container){
  unsigned int lineSize = 61;
  string tempStr;
  while(!container.empty()){
    tempStr = container.top();
    cout << '\\' << tempStr;
    container.pop();
  }
}


And these are the errors that get spit to me: MSVC
c:\program files\microsoft visual studio\vc98\include\algorithm(1264) : error
 C2784: '_D __cdecl std::operator -(const class 
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
 std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce tem
plate 
argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' 
from 'class std::list<class std::basic_string<char,struct
 std::char_traits<char>,class std::allocator<char> >,class std::allocator<class 
std::basic_string<char,struct std::

char_traits<char>,class std::allocator<char> > > >::iterator'

        c:\program files\microsoft visual studio\vc98\include\algorithm(1261) : 
see reference to function template instantiation 'void __cdecl std::_Push_heap_0
(class std::list<class std::basic_string<char,struct 
std::char_traits<char>,class std::al

locator<char> >,class std::allocator<class std::basic_string<char,struct 
std::char_traits<char>,class std::allocator<char> > > >::iterator,class 
std::list<class std::basic_string<char,struct std::char_traits<char>,class 
std::allocator<char> >,class 

std::allocator<class std::basic_string<char,struct std::char_traits<char>,class 
std::allocator<char> > > >::iterator,struct std::greater<class 
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> 
> >,int *,class std::basic

_string<char,struct std::char_traits<char>,class std::allocator<char> > *)' 
being compiled


gcc (g plus plus)
/usr/lib/gcc/i386-redhat-
linux/4.0.0/../../../../include/c++/4.0.0/bits/stl_heap.h: In function âvoid

 std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with 
_RandomAccessIterator = std::_List_iterator<std::string>, _Compare = 
std::greater<std::string>]â:


And there's much more where those two came from... I've done everything I can think of, including digging through the files mentioned (and how its _soo_ much fun looking through standard libraries...) This was part of a project to further my own understanding of how templates work, just a little frustrating when they dont ;) Any help, or guidence, is appreciated!

Share this post


Link to post
Share on other sites
Deyja    920
Compiling it with msvc8.0 gave me a much more descriptive error message.

It seems priority queu's internal container type must have random access iterators. std::list does not.

Just in case you're curious, the key error is

C:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1263) : error C2676: binary '-' : 'std::list<_Ty>::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator

Share this post


Link to post
Share on other sites
Rhalin    246
Quote:
Original post by Deyja
Compiling it with msvc8.0 gave me a much more descriptive error message.

It seems priority queu's internal container type must have random access iterators. std::list does not.

Just in case you're curious, the key error is

C:\Program Files\Microsoft Visual Studio 8\VC\include\algorithm(1263) : error C2676: binary '-' : 'std::list<_Ty>::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator


ARGH! I had this quiet lerking feeling that it was something remarkably simple!
I really need to get a good STL reference... I completely forgot about the container types the adapter is supposed to take.

Thanks for the help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this