小编sol*_*sol的帖子

为什么返回用普通括号内的大括号初始化的向量会导致编译错误?

我刚刚编写了一个简单的方法来返回由两个 int 参数组成的向量。但是,当我在普通括号内返回初始化的 int 向量时,它会导致编译错误。

\n
std::vector<int> getVec(int x, int y)\n{\n    return({x, y}); // This causes compile error\n    return {x, y};  // This is fine\n}\n
Run Code Online (Sandbox Code Playgroud)\n

错误消息显示:

\n
q.cpp: In function \xe2\x80\x98std::vector<int> getVec(int, int)\xe2\x80\x99:\nq.cpp:8:21: error: expected \xe2\x80\x98;\xe2\x80\x99 before \xe2\x80\x98}\xe2\x80\x99 token\n    8 |         return({x, y});\n      |                     ^\n      |                     ;\nq.cpp:8:15: error: could not convert \xe2\x80\x98((void)0, y)\xe2\x80\x99 from \xe2\x80\x98int\xe2\x80\x99 to \xe2\x80\x98std::vector<int>\xe2\x80\x99\n    8 |         return({x, y});\n      |               ^~~~~~~~\n      |               |\n      |               int\n
Run Code Online (Sandbox Code Playgroud)\n

c++ c++11

12
推荐指数
1
解决办法
1986
查看次数

标签 统计

c++ ×1

c++11 ×1