Hi,
I have a class provided to me called PQueue. It functions like a "priority queue"--elements can be enqueued in any order, but elements with higher priority are dequeued first.
The class templatized, implemented as a sorted singly-linked list (with the objects of highest priority at the front, while objects of lower priority are at the back. The class also uses a default comparison function:
As you can see, it would only work with int or string because it uses == and < to compare.Code:template <typename Type> int OperatorCmp(Type one, Type two) { if (one == two) return 0; else if (one < two) return -1; else return 1; }
The comparison function for a particular PQueue can be customized when invoking the constructor by passing in an alternative comparison function.
Code://constructor in PQueue.h PQueue(int (cmpFn)(Elem one, Elem two) = OperatorCmp); //there is a private member variable that is a pointer to the comparison function //(this also in pqueue.h) int (*cmpfn)(Elem one, Elem two);Now, I don't want my PQueue to hold int or string. I want to use the PQueue to hold a struct I defined called "path". So it would be: PQueue<path> myPath.Code://implementation of constructor in PQueue.cpp template <typename Elem> PQueue<Elem>::PQueue(int (cmpFn)(Elem one, Elem two)) { head = NULL; this->cmpfn = cmpFn; }
Here are my struct definitions, in case they might be helpful.
This means that I have to write a comparison function, which is here:Code:struct node; struct arc { double dist; node *link; }; struct node { string name; double x, y; Map<arc> connections; }; struct path { Vector<node> route; double dist; };
This comparison function resides in the driver file with main().Code:int comparePath(path path1, path path2) { if (path1.dist == path2.dist) return 0; if (path1.dist < path2.dist) return -1; return 1; }
The problem arises when I pass it into the constructor for PQueue in the driver file:
PQueue<path> myPath(comparePath);
I get errors when trying to compile referring me back to the default comparison function:
I'm at a loss now at what to do. I've been staring at this same thing for several hours now, trying different things and such, but to no avail.Code:Error 1 error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 2 error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 3 error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 4 error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 5 error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 6 error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 7 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 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 8 error C2676: binary '==' : 'node' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 23 Error 9 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24 Error 10 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24 Error 11 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24 Error 12 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24 Error 13 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 'node' c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24 Error 14 error C2676: binary '<' : 'node' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 8\vc\include\cs106cppinc\cmpfn.h 24
I noticed that I did not pass the arguments by reference in the custom comparison function that I made, so I changed that:
That reduced the amount of errors, somewhat:Code:int comparePath(path &path1, path &path2)
I'm not quite sure what's going wrong. Am I passing the custom comparison function correctly? Is there some kind of special template notation that I am missing?Code:Error 1 error C2664: 'PQueue<Elem>::PQueue(int (__cdecl *)(Elem,Elem))' : cannot convert parameter 1 from 'int (__cdecl *)(path &,path &)' to 'int (__cdecl *)(Elem,Elem)' 171 Error 2 error C2664: 'PQueue<Elem>::PQueue(int (__cdecl *)(Elem,Elem))' : cannot convert parameter 1 from 'int (__cdecl *)(path &,path &)' to 'int (__cdecl *)(Elem,Elem)' 183
Thanks in advance!



LinkBack URL
About LinkBacks


