小编atu*_*uly的帖子

在 std::map 中使用 std::less 无法编译

我无法理解为什么以下函数不能编译

#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)

c++ constructor most-vexing-parse c++11

4
推荐指数
1
解决办法
96
查看次数

cin并继续用逗号分隔一行

为什么以下代码无法编译?

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)

c++

3
推荐指数
1
解决办法
143
查看次数

标签 统计

c++ ×2

c++11 ×1

constructor ×1

most-vexing-parse ×1