未在此范围问题中声明

时间:2013-05-03 00:41:41

标签: c++

我正在检查并了解其他人的代码......代码在他的计算机中运行,但是当我尝试在我的机器中进行编辑时,它似乎无法正常工作。 lib文件都正确连接...

#include "play.h"
#include "Lib110ct/Lib110ct.h"
#include <ctime>


int main(int argc, char** argv)
{
Win110ct win;
Turtle * t = win.getTurtle();
win.hideTurtle();
const int NumOfBook = 7;
Player You(512,384);
Book B[NumOfBook];
int pts = 0;
char in;
time_t ti;
while (in != 'x')
{
    for (int i = 0; i<NumOfBook; i++)
    {
        if (A[i].isReadBy(You))
        {
            B[i].setPosition(rand() % 1024 + 1,rand() % 768 + 1);
            pts++;
        }
        B[i].move();
        B[i].draw(t);
    }
    You.draw(t);
    win << "Points: " << pts << "\n";
    win.render();
    win.setPosition(-1,-1);
    in = win.getchar();
    win.clearBack();
    win.clear();
    win.setPosition(0,0);
    if (in == 'w')
        You.moveRelative(0,-50);
    else if (in == 's')
        You.moveRelative(0,50);
    else if (in == 'a')
        You.moveRelative(-50,0);
    else if (in == 'd')
        You.moveRelative(50,0);
}
return 0;
}

但是我遇到了错误......“Book”未在此范围内声明。

标题play.h文件的代码如下..

#include "Lib110ct/Lib110ct.h"
#include <cstdlib>

//Player class, used to create the controllable character.
class Player
{
protected:
double xpos, ypos;
public:
Player(){};
//constructor, user input variables stored as xpos and ypos, then player is drawn
Player(double x, double y)
{
    xpos = x;
    ypos = y;
}

//draws the new position of the turtle
void draw(Turtle* t)
{
    t->setPosition(xpos,ypos);
    t->penDown();
    for (int i = 0; i < 180; i++ )
    {
        t->turn(2);
        t->moveForward(1);
    }
}


double getX(){ return xpos; } //returns the x position of the player
double getY(){ return ypos; } //returns the y position of the player

//moves the player by x increment and y increment (leave x or y as 0 to not move along the respective axis)
//relative to current position
void moveRelative(double xadd, double yadd)
{
    if (xpos+xadd > 0 && xpos+xadd < 1024 && ypos+yadd > 0 && ypos+yadd < 768)
    {
        xpos += xadd;
        ypos += yadd;
    }
}
};

//Food class. Not controlled by player
class Food
{
protected:
double xpos, ypos, xdir, ydir;
public:
//constructor, random x and y co-ordinates stored as xpos and ypos. food is drawn in       those co-ordinates
//random xdir and ydir, food initially moves in this direction
Food()
{
    xpos = rand() % 1024 + 1;
    ypos = rand() % 768 + 1;
    xdir = rand() % 41 - 20;
    ydir = rand() % 41 - 20;
}
//draws the food
void draw(Turtle* t)
{
    t->setPosition(xpos,ypos);
    t->penDown();
    for (int i = 0; i < 4; i++ )
    {
        t->turn(90);
        t->moveForward(20);
    }
}
double getX(){ return xpos; } //returns the x position of the player
double getY(){ return ypos; } //returns the y position of the player

//moves the food to a specific location, not relative to current position
void setPosition(double x,double y)
{
    xpos = x;
    ypos = y;
}

//moves the food in the specified directions, given by the variables xdir and ydir
void move()
{
    if (!(xpos+xdir>0 && xpos+xdir<1024 && ypos+ydir>0 && ypos+ydir<768))
    {
        xdir = -xdir;
        ydir = -ydir;
    }
    xpos += xdir;
    ypos += ydir;
}

//returns TRUE if the player is in a close proximity to the food, otherwise false
bool isEatenBy(Player Play)
{
    double pX = Play.getX();
    double pY = Play.getY();
    double fX = getX();
    double fY = getY();
    return pX+60>fX && pX-20<fX & pY+40>fY && pY-40<fY;
}
};

1 个答案:

答案 0 :(得分:1)

您正在尝试使用“Book”类型的数组,这可能是您要定义的类:

Book B[NumOfBook];

但是,您尚未在上面发布的代码或Dropbox中的代码中定义此内容。

将类Book的定义添加到play.h中,或者使用定义创建一个新的头文件(可能称为book.h),然后在cpp文件中创建#include“book.h”。您的Book类需要setPosition,move和draw的方法。