繁体   English   中英

为什么我的 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函数中返回ans2的值时,你需要通过ans2 = ask();在你的main函数中捕获它ans2 = ask(); . ideone 上的工作示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM