开发者

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

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

My code:

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) << "开发者_如何学Python 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.


I think the problem is that you've defined gamer as

int *gamer; 

So when you write

gamer->keyPress(key); 

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

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


Change

 int *gamer;

to

 Character* gamer;

and

 gamer; new Character(&drawArea, 0);

to

 gamer = new Character(&drawArea, 0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜