Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么std::map不能使用运算符[]:错误C2678?_C++_Stl_Std - Fatal编程技术网

C++ 为什么std::map不能使用运算符[]:错误C2678?

C++ 为什么std::map不能使用运算符[]:错误C2678?,c++,stl,std,C++,Stl,Std,具有: 这将导致以下错误: inline float getAreaCost(const int i) const { return m_areaCost[i]; } 我曾经认为,当我们调用[elementId]时,我们会得到元素值或默认元素值,所以我想知道这样简单的情况怎么会导致编译错误?因为您声明您的函数是常量,而[]不是常量运算符。 正如你自己所说的那样,[]!创建或返回新值。。如果无法创建,则不能使用此运算符。。我将使用find,然后返回.second值(如果有),类似于:

具有:

这将导致以下错误:

inline float getAreaCost(const int i) const { 
    return m_areaCost[i]; 
}

我曾经认为,当我们调用[elementId]时,我们会得到元素值或默认元素值,所以我想知道这样简单的情况怎么会导致编译错误?

因为您声明您的函数是常量,而[]不是常量运算符。 正如你自己所说的那样,[]!创建或返回新值。。如果无法创建,则不能使用此运算符。。我将使用find,然后返回.second值(如果有),类似于:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion) 

因为您声明您的函数是const,而[]不是const运算符。 正如你自己所说的那样,[]!创建或返回新值。。如果无法创建,则不能使用此运算符。。我将使用find,然后返回.second值(如果有),类似于:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const std::map<_Kty,_Ty>' (or there is no acceptable conversion) 
假定m_areaCost是getAreaCost所属对象的成员。但是,getAreaCost被标记为const成员函数。这意味着它不能对成员进行任何修改。因此,m_areaCost成员在此函数中是常量

不能在const std::map上调用运算符[],因为它的作用是在新元素尚不存在时插入新元素。而是使用:

假定m_areaCost是getAreaCost所属对象的成员。但是,getAreaCost被标记为const成员函数。这意味着它不能对成员进行任何修改。因此,m_areaCost成员在此函数中是常量

不能在const std::map上调用运算符[],因为它的作用是在新元素尚不存在时插入新元素。而是使用:


因为您将该方法标记为const

当您返回m_areaCost[i];如果键不存在,实际上可以在映射中创建条目,因此操作不是常量,因此映射的常量函数和非常量运算符[]之间不匹配


如果希望成员函数为常量,则必须使用查找键的条目。

,因为您将方法标记为常量

当您返回m_areaCost[i];如果键不存在,实际上可以在映射中创建条目,因此操作不是常量,因此映射的常量函数和非常量运算符[]之间不匹配

如果希望成员函数为const,则必须使用来查找键的条目。

原因是getAreaCost被声明为const,正如其他人所述,但我想给出一些更多的建议:

函数应始终返回有用的值,因此我建议getAreaCost为:

有几点: 1.输入参数是按值传递的,所以不需要常数inti,只需使用inti。 2.如果您发现像std::map::const_迭代器这样的东西很冗长,请给它一个typedef。如果你不这样做,当然可以继续写下去:P 3.0.0f只是一个例子,您可以返回任何适合您的情况的默认值。

原因是getAreaCost被声明为const,正如其他人所说,但我想给出一些更多的建议:

函数应始终返回有用的值,因此我建议getAreaCost为:

有几点: 1.输入参数是按值传递的,所以不需要常数inti,只需使用inti。 2.如果您发现像std::map::const_迭代器这样的东西很冗长,请给它一个typedef。如果你不这样做,当然可以继续写下去:P
3.0.0f只是一个例子,您可以返回任何适合您情况的默认值。

而m_areaCost[i]=成本;编译ok…=而m_areaCost[i]=成本;编译ok…=我将map定义为非常量simle类成员。@DuckQueen但您可以从常量方法访问它,因此只能调用其常量方法。我将map定义为非常量simle类成员。@DuckQueen但您可以从常量方法访问它,因此您只能调用其常量方法。
auto it = m_areaCost.find(i);
if (it != m_areaCost.end()) return (*it).second
return m_areaCost.at(i);
inline float getAreaCost(int i) const { 
    std::map<const int, float>::const_iterator It = m_areaCost.find(i);

    if (It != m_areaCost.end()) return It->second;

    return 0.0f;
}