2013-05-03 2 views
0

다른 사람의 코드를보고 이해하려고합니다. 컴퓨터에서 코드가 작동했지만 코드를 편집하려고 시도했지만 작동하지 않습니다. 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"이 (가) 선언되지 않았습니다.

Book B[NumOfBook]; 
다음과 같이

헤더 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; 
} 
}; 
+0

헤더 파일 "Book.h"가 누락 된 것 같습니다. – taocp

+0

음, 'Book'유형은 어디에 정의되어 있습니까? Lib110ct.h에서? '#include '에 대한 오류가 있습니까? 이것은 [SSCCE] (http://sscce.org/) – jerry

+0

https://www.dropbox.com/sh/6pzp6icfqy80xuo/bNGfgEOLuk와 멀리 떨어져 있습니다. 이것들은 오류가 있습니다 .. – user2339390

답변

1

당신은 당신이 정의하기위한 클래스가 아마 유형 '책'의 배열을 사용하려고 ..입니다

그러나 위에 게시 한 코드 또는 Dropbox에 넣은 코드에서이를 정의하지 않았습니다.

Play.h에 Book 클래스에 대한 정의를 추가하거나 cpp 파일에 #include "book.h"라는 새 헤더 파일 (예 : book.h)을 만듭니다. Book 클래스는 위치, 이동 및 그리기를 설정하는 메소드가 필요합니다.

+0

play.h에 배열을 추가하는 방법을 보여 주시겠습니까? – user2339390

+0

미안하지만, C++ 프로그래밍의 기초를 가르쳐 줄 수는 없다고 생각합니다. 자신에게 직접 간다면, 어디서 잘못되었는지 설명하려고 할 수 있습니다. – Muscles

관련 문제