Mfc stl映射常量迭代器和g++;错误
我正在尝试将一些windows的MFC类移植到linux,因为我必须将windows软件移植到linux 这是我需要移植的代码Mfc stl映射常量迭代器和g++;错误,mfc,g++,cstring,Mfc,G++,Cstring,我正在尝试将一些windows的MFC类移植到linux,因为我必须将windows软件移植到linux 这是我需要移植的代码 165: SMapCI it = _s$.find("nchairs"); 166: if (it==_s$.end()) return 10; 167: int n = strtoul(it->second.text.GetString(), NULL, 10); 和_$s和SMapCI的定义如下 typedef std::map<CString,
165: SMapCI it = _s$.find("nchairs");
166: if (it==_s$.end()) return 10;
167: int n = strtoul(it->second.text.GetString(), NULL, 10);
和_$s和SMapCI的定义如下
typedef std::map<CString, STablemapSymbol> SMap;
SMap _s$;
typedef SMap::const_iterator SMapCI;
我不理解这个错误。
g++文档说
passing 'const OBJECT' as 'this' argument of 'FUNCTION' discards qualifiers
*Message found in GCC version 4.5.1
*you're returning an address
*you're attempting to access a container element with a const_iterator using a member function that has no non-const versions. The non-const function does not guarantee it will not alter the data
但是…我的GetString函数被定义为“const char*”,所以我有关键字const。。。
所以我不明白…任何帮助都是非常受欢迎的
注意:我正在使用我自己的CString类,而不是直接用std::string对其进行更改,因为我要移植的代码太大了,我想对其进行最小的修改。(并且CString中定义的某些函数没有在std::string中定义)
提前感谢您的帮助!!您的函数签名应该是
const char*GetString()const
注意最后一个常量
现在,您会说,“我从一个非const CString实例返回一个const char指针”。这很好,但您要做的是定义一个可以在const CString实例上使用的函数-这就是最后一个const(在函数参数列表之后)指定可以在常量CString
上调用该函数
您可能需要该函数的两个版本,但特别是在这里需要它,因为
常量迭代器
将其内容公开为常量对象,而不管它们在容器本身中是什么。GetString的原型应该是:
const char *GetString() const;
第一个常量表示调用方无法修改返回值。第二个常量表示可以为常量CString
调用此方法
另一方面,我还将更改运算符<,并使用:
bool operator<(const char *_cstr)
bool运算符
const char *GetString() const;
bool operator<(const char *_cstr)