为什么我的 C++ 程序不工作?

Why isn't my C++ program working?

不知道为什么这不起作用,一切似乎都是正确的,但也许我遗漏了一些明显的东西,因为我刚刚开始了解 C++。

节目:

#include <iostream>
#include <string>
using namespace std;

string ask(){
    string ans2;
    cout << "Type:";
    cin >> ans2;
    return ans2;

}

int main()
{
    string ans2;
    string ans1="Hello";
    ask();
    cout << ans1 << " turns into " << ans2;
    return 0;
}

错误信息为:

Line 20:[Error] no match for call to '(std::string {aka     std::basic_string<char>}) (std::string&)'

Line 6:[Error] 'ans2' was not declared in this scope
Line 6:[Error] expected ',' or ';' before '{' token

mainask中的ans2是两个不同的变量。当您在 ask 函数中 return ans2 的值时,您需要通过 ans2 = ask();main 函数中捕获它。 Working example on ideone