嗨,我需要一些代码的帮助。我需要测试一个函数,但是每次尝试编译时都会出现编译器错误。这是我得到的错误:指针类型'void()()'和'const char'之间的比较缺少强制转换。
这是我的代码。

#include <iostream>
using namespace std;

void getInput();
bool gameGoing = true;

int main()
{
do{

    cout << "hello world this is a test.\n";
    getInput();
    if(getInput == "false")
    {
        return 0;
    }

}while(gameGoing = true);
}

void getInput()
{
string userInput;
cin >> userInput;
}

最佳答案

应该是

#include <iostream>
using namespace std;

string getInput();
bool gameGoing = true;

int main()
{
    do
    {

        cout << "hello world this is a test.\n";
        if(getInput() == "false")
            return 0;

    } while(gameGoing == true);
}


string getInput()
{
    string userInput;
    cin >> userInput;
    return userInput;
}

我改变了什么:
  • getInput函数中添加了返回类型,以便其结果不会被忽略
  • 修复了if比较,以再次实际比较getInput函数的结果和"false"
  • 修复了while条件,将gameGoingtrue值进行比较,而不是覆盖
  • 08-16 01:35