2

I am unable to compile below code in AIX whereas Linux and Solaris seem to be OK in this regard. Its giving me issues wrt to C++ Map usage and Key pair.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <map>
#define TRUE 1
#define FALSE 0

using namespace std;

std::pair<std::string, std::string> mDataMap[] =
{
    std::make_pair(std::string("HOME"), std::string("home")),
    std::make_pair(std::string("RETURNED"), std::string("Returned"))
};

size_t iSize = sizeof(mDataMap) / sizeof(mDataMap[0]);
std::map<std::string, std::string> mMap2;

const char* getValue(const char *key)
{
    std::map<std::string, std::string>::iterator it;

    it = mMap2.find(key);
    if (it != mMap2.end())
    {
        return (it->second).c_str();
    }
    return NULL;
}

int initMap()
{
    int retVal = 0;
    int i = 0;
    for (i = 0; i < iSize; i++)
    {
        mMap2[std::string(mDataMap[i].first.c_str())] = std::string(mDataMap[i].second.c_str());
    }
    if (i == iSize)
    {
        retVal = 1;
    }
    return retVal;
}

int main()
{
    initMap();

    std::map<std::string, std::string>::iterator mItr;

    // second insert function version (with hint position):
    mItr = mMap2.begin();

    cout << "\n Map contains \n ";
    for (mItr = mMap2.begin(); mItr != mMap2.end(); mItr++)
    {
        cout << mItr->first.c_str() << "    " << mItr->second.c_str() << endl;
    }

    const char *str = NULL;
    str = getValue("HOME");
    cout << "getValue(HOME) " << str << endl;
    str = getValue("RETURNED");
    cout << "getValue(RETURNED) " << str << endl;

    return 0;
}

The errors are bit cryptic as well

"/usr/vacpp/include/functional", line 155.29: 1540-0218 (S) The call does not match any parameter list for "operator<".
"/usr/vacpp/include/utility", line 92.14: 1540-1283 (I) "template <class _T1, class _T2> std::operator<(const pair<_T1,_T2> &, const pair<_T1,_T2> &)" is not a viable candidate.
"/usr/vacpp/include/functional", line 155.26: 1540-0288 (I) The function template parameter of type "const pair<_T1,_T2> &" cannot be initialized with an argument of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/xutility", line 324.14: 1540-1283 (I) "template <class _Ty, class _D, class _Pt, class _Rt, class _Pt2, class _Rt2> std::operator<(const _Ptrit<_Ty,_D,_Pt,_Rt,_Pt,_Rt> &, const _Ptrit<_Ty,_D,_Pt2,_Rt2,_Pt,_Rt> &)" is not a viable candidate.
"/usr/vacpp/include/functional", line 155.26: 1540-0288 (I) The function template parameter of type "const _Ptrit<_Ty,_D,_Pt,_Rt,_Pt,_Rt> &" cannot be initialized with an argument of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/xutility", line 470.14: 1540-1283 (I) "template <class _RI> std::operator<(const reverse_iterator<_RI> &, const reverse_iterator<_RI> &)" is not a viable candidate.
"/usr/vacpp/include/functional", line 155.26: 1540-0288 (I) The function template parameter of type "const reverse_iterator<_RI> &" cannot be initialized with an argument of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/xtree", line 478.14: 1540-1283 (I) "template <class _Tr> std::operator<(const _Tree<_Tr> &, const _Tree<_Tr> &)" is not a viable candidate.
"/usr/vacpp/include/functional", line 155.26: 1540-0288 (I) The function template parameter of type "const _Tree<_Tr> &" cannot be initialized with an argument of type "const std::basic_string<char,std::char_traits<char>,std::allocator<char> >".
"/usr/vacpp/include/functional", line 154.14: 1540-0700 (I) The previous message was produced while processing "std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >::operator()(const basic_string<char,std::char_traits<char>,std::allocator<char> > &, const basic_string<char,std::char_traits<char>,std::allocator<char> > &) const".
"/usr/vacpp/include/xtree", line 377.37: 1540-0700 (I) The previous message was produced while processing "std::_Tree<std::_Tmap_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,std::allocator<std::pair<const std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >,0> >::find(const k...".
"t3.cpp", line 27.15: 1540-0700 (I) The previous message was produced while processing "getValue(const char *)".

Can anyone suggest where i am going wrong OR how can i modify the code so that its very generic.

I am using xlc++_r on AIX for its compilation.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Following is a simplified version of above code which doesnt compile on AIX at all #include #include #include #include #include using namespace std; std::map myMap; int testFunc() { int retVal = 0; std::string key("Key"); std::string value("Value"); myMap.insert ( std::pair(key,value) ); return retVal; } int main() { testFunc(); return 0; } –  Aug 17 '18 at 08:34
  • 4
    One thing I notice is that you should have `#include ` instead of `string.h`. Possibly that could be the reason it can't find the < operator for string. – John Ilacqua Aug 17 '18 at 08:40
  • if you have a simplified fragment, please edit your post. – John Ilacqua Aug 17 '18 at 08:44
  • 2
    Bang on John IIacqua. missing #include was the core reason. Many thanks –  Aug 17 '18 at 08:53
  • @JohnIlacqua can you make your comment an answer please? It worked for me as well. – Gray Jul 21 '23 at 16:01

1 Answers1

1

You should have #include <string> as the correct std lib header instead of #include <string.h>.

John Ilacqua
  • 906
  • 5
  • 19