我刚刚编写了一个简单的方法来返回由两个 int 参数组成的向量。但是,当我在普通括号内返回初始化的 int 向量时,它会导致编译错误。
\nstd::vector<int> getVec(int x, int y)\n{\n return({x, y}); // This causes compile error\n return {x, y}; // This is fine\n}\nRun Code Online (Sandbox Code Playgroud)\n错误消息显示:
\nq.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\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1986 次 |
| 最近记录: |