C ++错误1错误C2227:' - > keyPress'的左边必须指向class / struct / union / generic类型

时间:2011-03-16 04:13:24

标签: c++ windows visual-c++-2008

您好我的代码有问题。我收到错误C2227。

我的代码:

Game.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

#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();
}

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

2 个答案:

答案 0 :(得分:1)

我认为问题在于您已将gamer定义为

int *gamer; 

所以当你写

gamer->keyPress(key); 

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

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

答案 1 :(得分:0)

更改

 int *gamer;

 Character* gamer;

 gamer; new Character(&drawArea, 0);

 gamer = new Character(&drawArea, 0);
相关问题