user2339390
user2339390

Reputation: 1

Was not declared in this scope issue

I'm checking to see and understand someone else's code... The code worked in his computer, however when I tried to edit it in mine, it doesn't seem to work. The lib files are all connected properly thou...

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

However I run into the error... "Book" was not declared in this scope.

The header play.h file's code is as follows..

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

Upvotes: 0

Views: 1145

Answers (1)

Muscles
Muscles

Reputation: 471

You are trying to use an array of type 'Book', which is probably a class you intended to define:

Book B[NumOfBook];

However, you have not defined this in the code you posted above, or in the code you put in Dropbox.

Either add a definition for class Book to play.h, or create a new header file, perhaps called book.h, with the definition and then #include "book.h" in your cpp file. Your Book class will need methods to setPosition, move and draw.

Upvotes: 1

Related Questions