C++ 错误C2446:“==”:没有从“int”到“int”的转换(u thiscall keyboard::*)(void)'

C++ 错误C2446:“==”:没有从“int”到“int”的转换(u thiscall keyboard::*)(void)',c++,function,class,C++,Function,Class,我的代码正常运行,但我在类中遇到以下错误:错误C2446:“==”:没有从“int”到“int”的转换\uu thiscall键盘::*void”。问题是: class keyboard { public: char W = 'W'; char A = 'A'; char S = 'S'; char D = 'D'; char E = 'E'; char tasta; int key_pressed_frd() { c

我的代码正常运行,但我在类中遇到以下错误:错误C2446:“==”:没有从“int”到“int”的转换\uu thiscall键盘::*void”。问题是:

class keyboard {

public:

    char W = 'W';
    char A = 'A';
    char S = 'S';
    char D = 'D';
    char E = 'E';
    char tasta;

    int key_pressed_frd() {
        cin >> tasta;
        if (tasta == W) return 1;
        else return 0;

    }

这是我的类的一部分,其中包括4个与此相同的函数,我得到了相同的错误

这就是我的全部代码:

#include <iostream>

using namespace std;

char tasta;

class keyboard {


public:

    int key_pressed_frd() {
        cin >> tasta;
        if (tasta == 'w') return 1;
        else return 0;

    }

    int key_pressed_back() {
        cin >> tasta;
        if (tasta == 's') return 1;
        else return 0;

    }

    int key_pressed_lft() {
        cin >> tasta;
        if (tasta == 'a') return 1;
        else return 0;

    }

    int key_pressed_rgt() {
        cin >> tasta;
        if (tasta == 'd') return 1;
        else return 0;

    }

    int key_pressed_stop() {
        cin >> tasta;
        if (tasta == 'e') return 1;
        else return 0;

    }
};

void forward() {

    cout << "I go forward!";
}

void back() {
    cout << "I go back!";
}

void left() {
    cout << "I go left!";
}

void right() {
    cout << "I go right!";
}

int main()
{
    //keyboard apas;

    while (true) {

        if (&keyboard::key_pressed_frd == 1)
            while (&keyboard::key_pressed_frd == 1) forward();

        if (&keyboard::key_pressed_back == 1)
            while (&keyboard::key_pressed_back == 1) back();

        if (&keyboard::key_pressed_lft == 1)
            while (&keyboard::key_pressed_lft == 1) left();

        if (&keyboard::key_pressed_rgt == 1)
            while (&keyboard::key_pressed_rgt == 1) right();

        if (&keyboard::key_pressed_stop == 1)
            while (&keyboard::key_pressed_stop == 1) break;

    }
}

它主要使用一个类从键盘获取输入,然后在确认条件后,控制台中将显示一条消息。我认为代码是非常错误的,因为这是我第一次使用类,但是如果您能提供帮助,我们将不胜感激!谢谢PS:tasta在我的语言中是指键盘上的一个随机键

要调用成员函数,请在其上附加空括号,如下所示:

...
if (keyboard::key_pressed_rgt() == 1)
...

函数名前的一个符号,如在&keyboard::key_pressed_rgt中,告诉编译器获取指向该函数的指针。这不是你需要的。编译器被诸如keyboard::key_pressed_rgt(没有括号)这样的代码弄糊涂了,并建议添加一个符号和。这不是一个好建议。

您在这里展示的代码似乎没有显示您描述的问题。请发布一个我们可以尝试的问题的最小复制品。除此之外,你的代码中有很多奇怪的东西。你在标题/文本中出现的错误,听起来像是您将int与返回int的成员函数指针进行了比较。可能是您忘记了将其放在成员函数后面以实际调用它吗?如果tasta==W,则可能是如果tasta==W,则可能不需要char W='W';还有它的朋友。你可以通过你的问题来澄清它。我是为你做的。