用{}初始化c++中的变量



我在Visual Studio Code中使用c++。当我想初始化一个变量时,我不能用{}初始化它(例如,int x{0};)。相反,我必须使用()(例如,int x(0);)。

当使用{}时,我得到的错误是&;error: expected ';'在声明的末尾&;;虽然我已经把;

我使用clang 11.0.0作为编译器。它与编译器有关吗?

代码通过。/filename命令在终端中运行。但是,当在VSCode中使用coderunner扩展运行时,它会给出错误。

要用{}初始化变量,必须声明它是={}。

:

int x = {3}; //you wouldn't really do this for simple variable though I don't think since you can just say int x = 3;
//or if you are making a custom object you might say:
MyObject object = {"apples", 3, "red"};
// to set the variables inside the object. in the order they are declared.
我希望这能回答你的问题!

最新更新