【发布时间】:2013-12-13 08:37:33
【问题描述】:
我有一个Item 类,它可以携带多种类型的数据,包括std::map<Item, Item>。
我已定义operator[] 来执行以下操作:如果 Item 不是 V_MAP 类型(V_MAP 是我指定携带类型的枚举的一部分),则 Item 将其类型更改为 V_MAP。在这个“演员”之后,它返回internalMap [ ItemPassedAsArgument ]。该运算符具有以下声明:
Item& operator[] (const Item& itemArg);
效果很好,我已经通过各种方式进行了测试。
现在,这需要与其他一些东西集成,首先是 ClassX。
ClassX 有一个 Item(V_MAP) 类型的成员。它需要容纳东西。它(ClassX)还有一个成员方法定义如下:
std::string ClassX::getSmth () const {
return item[keySmth];
}
keySmth 在哪里
const std::string ClassX::keySmth ("blablabla");
我不允许更改getSmth() 方法的定义。那个 const 需要留在那里,那个方法应该只检查东西。
如您所见,该方法无法更改任何内容,因此与我的运算符相矛盾。像这样使用时,错误提示为No operator[] matches these operands.Operands are const Item [ const std::string ]。
因此,我重载了operator[] 来做几乎相同的事情,除了它改变Item 的类型并返回一个常量Item 的部分。这是它的声明:
const Item& operator[] (const Item& itemArg) const;
现在由程序员来确保下标运算符现在仅应用于 V_MAP 类型的 Items。
但现在我得到以下编译器错误:
error C2668: 'std::basic_string<_Elem,_Traits,_Alloc>::basic_string' : ambiguous call to overloaded function
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(896): could be 'std::basic_string<_Elem,_Traits,_Alloc>::basic_string(std::basic_string<_Elem,_Traits,_Alloc> &&) throw()'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(789): or 'std::basic_string<_Elem,_Traits,_Alloc>::basic_string(const _Elem *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
while trying to match the argument list '(const Item)'
问题出在哪里? 谢谢
编辑:这是 std::string 的构造函数:
Item(const std::string str_value) {
m_data = new Data<std::string> (str_value);
m_type = V_STR;
}
std::string 来自Item:
template<typename T>
operator T () const {
//Check
if (this->m_data == nullptr)
return NULL;
// dynamic_cast m_data to point to "an Item of type T"
Data<T>* temp_data = dynamic_cast<Data<T>* > (m_data);
return temp_data->m_dataOfAnyType;
}
【问题讨论】:
-
如何从
Item创建std::string?这就是相关的代码部分。 -
我已经编辑了问题,你可以在它的末尾找到构造函数
-
我们不需要
std::string到Item,而是Item到std::string -
对不起,读得太快了:P