我尝试运行代码时在vscode中遇到奇怪的错误

时间:2020-05-05 07:03:49

标签: c++ visual-studio-code

当我尝试运行一个简单的hello world脚本时,它会发送此错误消息,有人可以帮助我解决此问题吗?

[Running] cd "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\" && g++ helloworld.cpp -o helloworld && "c:\Users\NickT\OneDrive\Documents\C++ Tutorial\"helloworld
helloworld.cpp: In function `int main()':
helloworld.cpp:9: error: expected primary-expression before "msg"
helloworld.cpp:9: error: expected `;' before "msg"
helloworld.cpp:11: error: expected primary-expression before "const"
helloworld.cpp:11: error: expected `;' before "const"
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `)' before '}' token
helloworld.cpp:16: error: expected primary-expression before '}' token
helloworld.cpp:16: error: expected `;' before '}' token

[Done] exited with code=1 in 0.233 seconds

这是书面代码

#include <iostream>

int main(){
    std::cout << "Hello World" << std::endl;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我修改了您发布的代码,并添加了一段代码以利用命名空间std。分析编译器的输出,似乎您可能还没有添加分号。另外,调用标准函数时不必引用std库,但这取决于您的偏好。似乎也不是您的代码有问题。您能否发布您尝试编译的实际代码,因为这似乎没有问题。

修改后的代码:

#include <iostream>

using namespace std;

int main() {
  std::cout << "Hello World" << std::endl;
  return 0;
}