我无法理解为什么以下函数不能编译
#include <iostream>
#include <map>
int main(){
std::map<int, int, std::less<int>> myMap(std::less<int>());
myMap[2] = 2;
std::cout << myMap[2] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息如下——
std_less_check.cpp: In function ‘int main()’:
std_less_check.cpp:6:10: warning: pointer to a function used in arithmetic [-Wpointer-arith]
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: assignment of read-only location ‘*(myMap + 2)’
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: cannot convert ‘int’ to ‘std::map<int, int, std::less<int> >(std::less<int> (*)())’ in assignment
std_less_check.cpp:7:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
std::cout << …Run Code Online (Sandbox Code Playgroud) 为什么以下代码无法编译?
for(int i = 0; i < 10; i++){
if(i == 5) i = 7, continue;
}
Run Code Online (Sandbox Code Playgroud)
逗号可以用作分隔符和运算符.如果它像operator一样运行,它会计算两个表达式并返回最后一个表达式.我知道它不是作为运算符在这里,因为continue不是操作数.但为什么它不是两个陈述的分隔符.它显示以下错误 -
error: expected primary-expression before ‘continue’
if(i == 5) i = 7, continue;
Run Code Online (Sandbox Code Playgroud)