473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Compile error on templated STL map class

4
I have a class template (ExclusiveMap) which takes two type parameters C1 and C2 and declares two private members map<C1, C2> and map<C2, C1> (both STL maps). In test code I instantiate this class template providing types int and string for C1 and C2 respectively.

The test code compiles cleanly using GCC 3.2 however when I pass it through HPUX aCC I get error messages about the inability to convert strings to ints.

I can appreciate that aCC might be stricter than GCC in certain aspects however I cannot understand how GCC would compile and link it with no errors or warnings resulting in an executable that actually does what I want whereas aCC quits with errors, not just warnings.

Can anyone see what might be causing aCC to require conversion from string to int in the code below?

test.cc:
=====

Expand|Select|Wrap|Line Numbers
  1. #include <ExclusiveMap.h>
  2. #include <string>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. class FloeLog
  7. {
  8.  
  9. public:
  10.  
  11.     void init();
  12.  
  13. private:
  14.  
  15.      ExclusiveMap<int, string>  _logTypeMap;  // aCC does not seem to like
  16. //    ExclusiveMap<int, int>  _logTypeMap;  // No compile errors
  17.  
  18. }; // class FloeLog
  19.  
  20. void FloeLog::init()
  21. {
  22.     cout << "Inside FloeLog::init(), about to do _logTypeMap.insert()\n";
  23.  
  24.       _logTypeMap.insert(12, "FLOE_EVENT");
  25. //    _logTypeMap.insert(12, 13); // No compile errors
  26.  
  27. }
  28.  
  29. int main()
  30. {
  31.  
  32. FloeLog FL;
  33.  
  34. FL.init();
  35.  
  36. return 0;
  37.  
  38. }
ExclusiveMap.h:
============
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. using namespace std;
  3.  
  4. template <class C1, class C2>
  5. class ExclusiveMap
  6. {
  7. public:
  8.  
  9.     ExclusiveMap();
  10.  
  11.     bool insert(const C1& c1, const C2& c2);
  12.  
  13. private:
  14.  
  15.     map<C1, C2> _map1;
  16.     map<C2, C1> _map2;
  17. };
  18.  
  19. template <class C1, class C2>
  20. ExclusiveMap<C1, C2>::ExclusiveMap()
  21. {
  22. }
  23.  
  24. template <class C1, class C2>
  25. bool
  26. ExclusiveMap<C1, C2>::insert(const C1& c1, const C2& c2)
  27. {
  28.  
  29.      typename map<C1, C2>::iterator map1Iter = _map1.find(c1);
  30.      typename map<C2, C1>::iterator map2Iter = _map2.find(c2); // This causes compile error when using HP aCC
  31.  
  32.      if (map1Iter ==  _map1.end() && map2Iter == _map2.end())
  33.      {
  34.        _map2[c2] = c1;
  35.        _map1[c1] = c2;
  36.         return true;
  37.      }
  38.     return false;
  39. }
% g++ -g -I. test.cc
<compiles and links without errors>


% /opt/aCC/bin/aCC -AA -c -g -I. test.cc
Error 226: "/opt/aCC-3.30.01/include_std/rw/tree.cc", line 492 # No appropriate function found for call of 'operator ()'. Last viable
candidate was "bool std::less<int>::operator ()(const int &,const int &) const" ["/opt/aCC-3.30.01/include_std/functional", line
167]. Argument of type 'const std::basic_string<char,std::char_traits<char>,std: :allocator<char> > &' could not be converted to
'const int &'.
if (!_C_key_compare(__x->_C_key(), __k))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error 226: "/opt/aCC-3.30.01/include_std/rw/tree.cc", line 500 # No appropriate function found for call of 'operator ()'. Last viable
candidate was "bool std::less<int>::operator ()(const int &,const int &) const" ["/opt/aCC-3.30.01/include_std/functional", line
167]. Argument of type 'const std::basic_string<char,std::char_traits<char>,std: :allocator<char> > &' could not be converted to
'const int &'.
|| _C_key_compare(__k, ((_C_tree_iter&)__j)._C_node->_C_key())) ? e
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
Error 556: "./ExclusiveMap.h", line 30 # Unable to generate specialization "__rw::__rw_tree_iter<std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char> >,int>,long,std::pair<const int,int> *,std::pair<const int,int>
&,__rw::__rw_rb_tree_node<std::allocator<std::pair <const int,int> >,std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,int>,std::basic_string<char,std::char_traits<cha r>,std::allocator<char> >,__rw::__select1st<std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,int>,std::basic_string<char,std::char_traits<cha r>,std::allocator<char> > > > >
__rw::__rb_tree<std::basic_string<char,std::char_t raits<char>,std::allocator<char> >,std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char> >,int>,__rw::__select1st<std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,int>,std::basic_string<char,std::char_traits<cha r>,std::allocator<char> > >,std::less<int>,std::allocator<std::pair<const
int,int> > >::find(const std::basic_string<char,std::char_traits<char>,std: :allocator<char> > &)" due to errors during generation.
typename map<C2, C1>::iterator map2Iter = _map2.find(c2); // This causes compile error when using HP aCC
^^^^^^^^^^^^^^
Error 440: "/opt/aCC-3.30.01/include_std/utility", line 117 # Cannot initialize 'const int' with 'const class
basic_string<char,std::char_traits<char>,std::allo cator<char> >'.
: first (__rhs.first), second (__rhs.second) { }
^^^^^^^^^^^
Aug 11 '10 #1
7 3476
newb16
687 512MB
Apparently this compiler's library lacks 'operator <' for std::string, so it can't be used as map key without defining comparer. Does
Expand|Select|Wrap|Line Numbers
  1.  std::string a,b;
  2. a<b;
  3.  
compile?
Aug 11 '10 #2
Arjuna
4
Thanks for your reply. I agree that the error messages do suggest so. A quick test along the lines you've outlined however shows that the '<' operator does indeed exist for std::string (i.e. aCC compiles your code snippet w/o error).

I trimmed my original code down to its barest minimum such that aCC still errors out on it complaining about type mismatches (while g++ still compiles it fine).

Test.cc:

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <class C1, class C2>
  6. class ExclusiveMap
  7. {
  8.     map<C1, C2> _map1;
  9.     map<C2, C1> _map2;
  10. };
  11.  
  12. class FloeLog
  13. {
  14.  
  15.      ExclusiveMap<int, string>  _logTypeMap;  
  16. //   ExclusiveMap<string, int>  _logTypeMap2;  
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.  
  23. FloeLog FL;
  24. return 0;
  25.  
% g++ -g -I. Test.cc
<compiles and links without errors>

% /opt/aCC/bin/aCC -AA -g -I. Test.cc
Error (future) 212: "/opt/aCC-3.30.01/include_std/rw/tree", line 454 # Argument type 'const std::pair<const int,int> *' does not match
expected parameter type 'std::pair<const int,int> *'.
_RWSTD_VALUE_ALLOC
^^^^^^^^^^^^^^^^^^
Error 440: "/opt/aCC-3.30.01/include_std/utility", line 117 # Cannot initialize 'const int' with 'const class
basic_string<char,std::char_traits<char>,std::allo cator<char> >'.
: first (__rhs.first), second (__rhs.second) { }
^^^^^^^^^^^
Error 445: "Test.cc", line 20 # Cannot recover from earlier errors.
int main()
^^^^^^^^^^


If I swap the order of passing types <int, string> to be <string, int> like this:

Test.cc:

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <class C1, class C2>
  6. class ExclusiveMap
  7. {
  8.     map<C1, C2> _map1;
  9.     map<C2, C1> _map2;
  10. };
  11.  
  12. class FloeLog
  13. {
  14.  
  15. //    ExclusiveMap<int, string>  _logTypeMap;  
  16.       ExclusiveMap<string, int>  _logTypeMap2;  
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.  
  23. FloeLog FL;
  24. return 0;
  25.  
% /opt/aCC/bin/aCC -AA -g -I. Test.cc
Error (future) 212: "/opt/aCC-3.30.01/include_std/rw/tree", line 454 # Argument type 'const std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > > *' does not match expected parameter type
'std::pair<const std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > > *'.
_RWSTD_VALUE_ALLOC
^^^^^^^^^^^^^^^^^^
Error 226: "/opt/aCC-3.30.01/include_std/utility", line 117 # No appropriate function found for call of 'basic_string::basic_string'.
Last viable candidate was "std::basic_string<char,std::char_traits<char>,std ::allocator<char> >::basic_string(const
std::allocator<char> &)" ["/opt/aCC-3.30.01/include_std/string", line 204]. Argument of type 'const int' could not be converted to
'const std::allocator<char> &'.
: first (__rhs.first), second (__rhs.second) { }
^^^^^^^^^^^
Error 445: "Test.cc", line 20 # Cannot recover from earlier errors.
int main()
^^^^^^^^^^

i.e. a slightly different error message but essentially the same thing.

Now here's the really strange thing: If I comment out the second map instantiation map<C2, C1> _map2 then aCC compiles and links Test.cc with no errors emitted.

Test.cc:

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <class C1, class C2>
  6. class ExclusiveMap
  7. {
  8.     map<C1, C2> _map1;
  9. //    map<C2, C1> _map2;
  10. };
  11.  
  12. class FloeLog
  13. {
  14.  
  15.       ExclusiveMap<int, string>  _logTypeMap;  
  16.       ExclusiveMap<string, int>  _logTypeMap2;  
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.  
  23. FloeLog FL;
  24. return 0;
  25.  
% /opt/aCC/bin/aCC -AA -g -I. Test.cc
<compiles and links without errors>

Given the symmetry in that I'm instantiating two ExclusiveMap classes, one with <int, string> and the other with <string, int> you'd think that if the above compiles cleanly then so too should the following where I've commented out _map1 instead of _map2:

Test.cc:

Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <class C1, class C2>
  6. class ExclusiveMap
  7. {
  8. //    map<C1, C2> _map1;
  9.     map<C2, C1> _map2;
  10. };
  11.  
  12. class FloeLog
  13. {
  14.  
  15.       ExclusiveMap<int, string>  _logTypeMap; 
  16.       ExclusiveMap<string, int>  _logTypeMap2; 
  17. };
  18.  
  19.  
  20. int main()
  21. {
  22.  
  23. FloeLog FL;
  24. return 0;
  25.  
But no.

% /opt/aCC/bin/aCC -AA -g -I. Test.cc
Error (future) 212: "/opt/aCC-3.30.01/include_std/rw/tree", line 454 # Argument type 'const std::pair<const
std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > > *' does not match expected parameter type
'std::pair<const std::basic_string<char,std::char_traits<char>,std: :allocator<char>
>,std::basic_string<char,std::char_traits<char>,st d::allocator<char> > > *'.
_RWSTD_VALUE_ALLOC
^^^^^^^^^^^^^^^^^^
Error 226: "/opt/aCC-3.30.01/include_std/utility", line 117 # No appropriate function found for call of 'basic_string::basic_string'.
Last viable candidate was "std::basic_string<char,std::char_traits<char>,std ::allocator<char> >::basic_string(const
std::allocator<char> &)" ["/opt/aCC-3.30.01/include_std/string", line 204]. Argument of type 'const int' could not be converted to
'const std::allocator<char> &'.
: first (__rhs.first), second (__rhs.second) { }
^^^^^^^^^^^
Error 445: "Test.cc", line 20 # Cannot recover from earlier errors.
int main()
^^^^^^^^^^


Needless to say I am completely baffled by what I am seeing and at this stage I can think of no other cause than a bug in aCC.

I would be very interested to hear if anyone feels otherwise - thanks.
Aug 11 '10 #3
newb16
687 512MB
googling for "include_std/rw/tree" makes me think so.
http://software.itags.org/linux-unix/243203/
Aug 11 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
I'm probably missing the whole point but your code compiles and links using Visual Studio.NET 2008.
Aug 11 '10 #5
Arjuna
4
@newb16 Thanks mate, I am now convinced it is a bug in the version of aCC that I'm using. Sadly I'm unable to upgrade to a more recent version.
Aug 12 '10 #6
Arjuna
4
@weaknessforcats Yeah the problem is that HPUX aCC version A.03.31 that I'm stuck with doesn't like the code even though every other compiler seems to have no problem compiling it.
Aug 12 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
Visual Studio gets its STL templates from PJ Plauger at Dinkumware.

Have you checked that you are using the latest set of STL templates for your compiler?
Aug 12 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Payne | last post by:
Consider this (templated) class member function: template<typename Type> void CircularContainer<Type>::insert(const Type& s) { vector<Type>::iterator itr = find(m_elements.begin(),...
4
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
3
by: Fiber Optic | last post by:
I wrote a sample app to test using a function to obtain friend-level access to a templated class. However, I am getting similar errors from two separate compilers. If anyone can recommend what...
1
by: Vince | last post by:
Hi, I have a templated class implemented in one file called CDynWnd.h and declared like this : CDynWnd.h (declaration and implementation) ------------ template <class BASECLASS> class...
6
by: __PPS__ | last post by:
it's ok according to the standard to specialize member function like this: template<class T>struct xxx { void func(); }; template<>bool xxx<int>::func(){ //int version } template<>bool...
4
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access...
4
by: Jim Langston | last post by:
This should illistrate what I am trying to do: template <class T> T SomeFunction( T parm ) { return parm; } template <class T> class SomeClass
2
by: musikit | last post by:
I have my own templated vector class and im trying to use my vector class in another templated class. im using GCC 4.0.1 on Mac OSX (if i need to upgrade that isnt a problem) but i want to know if...
3
card
by: card | last post by:
Ah, the joy of generic programming. Ok. Here's my problem. I have a templated class with a internally defined structure type. Within the class, I have some member functions whose return value is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.