windows C++ 错误 1 ​​错误 C2227:'->keyPress' 的左侧必须指向类/结构/联合/泛型类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5320958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:26:56  来源:igfitidea点击:

C++ Error 1 error C2227: left of '->keyPress' must point to class/struct/union/generic type

c++windowsvisual-c++-2008

提问by Blackelfwolf

Hi I am having trouble with my code. I got error C2227.

嗨,我的代码有问题。我收到错误 C2227。

My code:

我的代码:

Game.h

游戏.h

#ifndef GAME_H
#define GAME_H
#include "drawEngine.h"
#include "Sprite.h"


class Runner
{
public:
    bool run();



    Runner(){};
protected:
    bool getInput(char *c);

    void timerUpdate();
private:
    int *gamer;
    double frameCount;
    double startTime;
    double lastTime;

    int posX;



    drawEngine drawArea;
};

#endif

Game.cpp

游戏.cpp

#include "Game.h"
#include <conio.h>
#include <iostream>
#include "drawEngine.h"
#include "Character.h"
#include <windows.h>
using namespace std;
//this will give ME 32 fps
#define GAME_SPEED 25.33
bool Runner::run()
{

    drawArea.createSprite(0, '$');
    gamer; new Character(&drawArea, 0);


    char key = ' ';

    startTime = timeGetTime();

    frameCount = 0;
    lastTime = 0;

    posX = 0;

    while (key != 'q')
    {
        while(!getInput(&key))
        {
            timerUpdate();
        }

        gamer->keyPress(key);
        //cout << "Here's what you pressed: " << key << endl;
    }

    delete gamer;
    cout << frameCount / ((timeGetTime() - startTime) / 100) << " fps " << endl;
    cout << "Game Over" << endl;

    return true;
}

bool Runner::getInput(char *c)
{ 
    if (kbhit())
    {
        *c = getch();
        return true;
    }
}

void Runner::timerUpdate()
{
    double currentTime = timeGetTime() - lastTime;

    if (currentTime < GAME_SPEED)
        return;


    frameCount++;

    lastTime = timeGetTime();
}

I've never seen this before. I've looked everywhere for an answer but they don't work with my code. I've got other code too that belongs to the same project which I didn't post.

我以前从未见过这个。我到处寻找答案,但它们不适用于我的代码。我也有其他代码属于我没有发布的同一个项目。

采纳答案by Rob Agar

Change

改变

 int *gamer;

to

 Character* gamer;

and

 gamer; new Character(&drawArea, 0);

to

 gamer = new Character(&drawArea, 0);

回答by templatetypedef

I think the problem is that you've defined gameras

我认为问题在于你已经定义gamer

int *gamer; 

So when you write

所以当你写

gamer->keyPress(key); 

You're trying to call a member function on an int, which is not legal.

您试图在 上调用成员函数int,这是不合法的。

Are you sure you want gamerto be an int *? That seems incorrect.

您确定要gamer成为int *吗?那似乎不正确。