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

Why isn't my C++ program working?

本文关键字:不起作用 程序 C++ 我的 为什么      更新时间:2023-10-16

不知道为什么这不起作用,一切似乎都是对的,但也许我在了解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函数中返回ans2的值时,您需要通过ans2 = ask();main函数中捕获它。Videone 的工作示例