类型映射....不提供呼叫运营商
Type map.... does not provide a call operator
尝试在 class 月实现一些重载运算符:
class Month
{
int monthNumber;
string name;
}
我能够毫无问题地实现以下构造函数:
Month::Month(int cust_month)
{
monthNumber = cust_month;
name = int_month.at(cust_month);
}
注意映射(未显示)int_month 的使用,其中 int 1-12 映射到相应的月份名称,这工作正常。但是在重载 ++ 运算符时试图做类似的事情:
Month Month::operator++() {
if (monthNumber == 12) {
monthNumber = 1;
name = "January";
}
else{
++monthNumber;
name = int_month(monthNumber); // ERROR
}
return *this;
}
在上面的代码片段中,int_month 被突出显示并显示错误:
Type 'map<int, std::__1::string>' (aka 'map<int, basic_string<char, char_traits<char>, allocator<char> > >') does not provide a call operator
我读过类似的帖子,它们都解决了某种编程错误,但在阅读它们之后,我仍然不确定这个错误对我的代码意味着什么。我不仅很好奇如何解决它,而且很好奇为什么在我的构造函数中使用 map 按键赋值工作正常,但相同的过程不能使运算符过载。
当您阅读错误时,它会准确地告诉您问题所在:
Type 'map<int, std::__1::string>' (aka 'map<int, basic_string<char, char_traits<char>, allocator<char> > >') does not provide a call operator
您有一个从 int 到 string 的映射,它不提供调用运算符。这意味着没有函数std::map<...>::operator()(...)
。所以你要做的是你应该去参考像cppreference你会看到没有operator()
.
此外,您会看到有一个部分元素访问,它为您提供了两个功能:
at
: 使用边界检查访问指定元素
operator[]
: 访问或插入指定元素
这非常准确地告诉您存在哪些函数。如果您更详细地检查这些,您还会看到 function signature,例如 operator[]
:
T& operator[]( const Key& key );
T& operator[]( Key&& key );
这意味着您可以将右值或对左值的 const 引用传递给括号运算符。最后还要仔细阅读文档和代码示例,以了解如果值不存在则将值插入到 map
中(与 at
相反,如果元素不存在则会抛出).
最后,一些用法示例:
std::map<int, std::string> m;
m[3] = "a"; // inserts "a" at position 3
std::cout << m[3]; // prints "a"
m[3] = "b"; // modifies the conetent at 3 to "b"
std::cout << m[3]; // prints "b"
m.at(3) = "c"; // modifies the content at 3 to "c"
std::cout << m[3]; // prints "b"
m.at(4) = "d"; // this will throw std::out_of_range
尝试在 class 月实现一些重载运算符:
class Month
{
int monthNumber;
string name;
}
我能够毫无问题地实现以下构造函数:
Month::Month(int cust_month)
{
monthNumber = cust_month;
name = int_month.at(cust_month);
}
注意映射(未显示)int_month 的使用,其中 int 1-12 映射到相应的月份名称,这工作正常。但是在重载 ++ 运算符时试图做类似的事情:
Month Month::operator++() {
if (monthNumber == 12) {
monthNumber = 1;
name = "January";
}
else{
++monthNumber;
name = int_month(monthNumber); // ERROR
}
return *this;
}
在上面的代码片段中,int_month 被突出显示并显示错误:
Type 'map<int, std::__1::string>' (aka 'map<int, basic_string<char, char_traits<char>, allocator<char> > >') does not provide a call operator
我读过类似的帖子,它们都解决了某种编程错误,但在阅读它们之后,我仍然不确定这个错误对我的代码意味着什么。我不仅很好奇如何解决它,而且很好奇为什么在我的构造函数中使用 map 按键赋值工作正常,但相同的过程不能使运算符过载。
当您阅读错误时,它会准确地告诉您问题所在:
Type 'map<int, std::__1::string>' (aka 'map<int, basic_string<char, char_traits<char>, allocator<char> > >') does not provide a call operator
您有一个从 int 到 string 的映射,它不提供调用运算符。这意味着没有函数std::map<...>::operator()(...)
。所以你要做的是你应该去参考像cppreference你会看到没有operator()
.
此外,您会看到有一个部分元素访问,它为您提供了两个功能:
at
: 使用边界检查访问指定元素operator[]
: 访问或插入指定元素
这非常准确地告诉您存在哪些函数。如果您更详细地检查这些,您还会看到 function signature,例如 operator[]
:
T& operator[]( const Key& key );
T& operator[]( Key&& key );
这意味着您可以将右值或对左值的 const 引用传递给括号运算符。最后还要仔细阅读文档和代码示例,以了解如果值不存在则将值插入到 map
中(与 at
相反,如果元素不存在则会抛出).
最后,一些用法示例:
std::map<int, std::string> m;
m[3] = "a"; // inserts "a" at position 3
std::cout << m[3]; // prints "a"
m[3] = "b"; // modifies the conetent at 3 to "b"
std::cout << m[3]; // prints "b"
m.at(3) = "c"; // modifies the content at 3 to "c"
std::cout << m[3]; // prints "b"
m.at(4) = "d"; // this will throw std::out_of_range