C++ 如何从c+;中的函数返回字符串对象+;没有收到需要分号的编译错误?

C++ 如何从c+;中的函数返回字符串对象+;没有收到需要分号的编译错误?,c++,C++,当我试图返回一个字符串时,我收到一个编译错误,表示在调用“hello()”之前需要一个分号 我的代码如下(包括iostream和string头)。请帮助我理解此代码中的错误 std::string hello() { std::string word = "hello world"; return word; } int main() { std::string userWord = ""; userWord hello(); std::cout <<

当我试图返回一个字符串时,我收到一个编译错误,表示在调用“hello()”之前需要一个分号

我的代码如下(包括iostream和string头)。请帮助我理解此代码中的错误

std::string hello()
{
   std::string word = "hello world";
   return word;
}

int main()
{
   std::string userWord = "";

   userWord hello();
   std::cout << userWord << std::endl;
   std::cin.get();

   return 0;
}
std::string hello()
{
std::string word=“hello world”;
返回词;
}
int main()
{
std::string userWord=“”;
userwordhello();
std::cout试试看

或者更好

std::string userWord(hello());  // construct userWord to have the right value
难道不是吗

string userWord = hello();

或者怎么样呢?
std::string userWord=hello();谢谢。这是正确的。它应该是:std::string userWord=hello();
string userWord = hello();