P
pmatos
Hi all,
I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>
using namespace std;
class X {
public:
void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
private:
struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 < *s2;
}
};
map<const string *, unsigned int, ltstr> strCodeMap;
};
int main() {
X c;
const string * s1 = new string("Hello");
const string * s2 = new string("Buh");
c.insert(s1, 3);
c.insert(s2, 4);
const string * s4 = new string("Hello");
cout << "S4 VALUE: " << c.getVarCode(s4) << "\n";
delete s1;
delete s2;
delete s4;
return 0;
}
The compile time error is in the public method of class X:
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
And is:
$ g++ maptest.cc -o maptest
maptest.cc: In member function `unsigned int X::getVarCode(const
std::string*)
const':
maptest.cc:10: error: passing `const std::map<const std::string*,
unsigned int,
X::ltstr, std::allocator<std:air<const std::string* const,
unsigned int> >
_Tp =
unsigned int, _Compare = X::ltstr, _Alloc =
std::allocator<std:air<const
std::string* const, unsigned int> >]' discards qualifiers
Can someone give me a hint on what this means?
Cheers,
Paulo Matos
I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>
using namespace std;
class X {
public:
void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
private:
struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 < *s2;
}
};
map<const string *, unsigned int, ltstr> strCodeMap;
};
int main() {
X c;
const string * s1 = new string("Hello");
const string * s2 = new string("Buh");
c.insert(s1, 3);
c.insert(s2, 4);
const string * s4 = new string("Hello");
cout << "S4 VALUE: " << c.getVarCode(s4) << "\n";
delete s1;
delete s2;
delete s4;
return 0;
}
The compile time error is in the public method of class X:
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
And is:
$ g++ maptest.cc -o maptest
maptest.cc: In member function `unsigned int X::getVarCode(const
std::string*)
const':
maptest.cc:10: error: passing `const std::map<const std::string*,
unsigned int,
X::ltstr, std::allocator<std:air<const std::string* const,
unsigned int> >
_Alloc>:perator[](const _Key&) [with _Key = const std::string*,>' as `this' argument of `_Tp& std::map<_Key, _Tp, _Compare,
_Tp =
unsigned int, _Compare = X::ltstr, _Alloc =
std::allocator<std:air<const
std::string* const, unsigned int> >]' discards qualifiers
Can someone give me a hint on what this means?
Cheers,
Paulo Matos