2013-06-25 4 views
2

저는 프로그래밍 및 C++에 대한 새로운 지식을 갖고 있으며 Allegro 5로 게임을 프로그래밍하는 방법을 배웁니다. 내가 직접 설정 한 프로젝트 중 하나는 정리하는 것입니다. 여기에서 찾은 Pong의 튜토리얼 소스 코드 : http://www.cppgameprogramming.com/newforums/viewtopic.php?f=5&t=1991클래스 선언 오류 : 형식을 결정하기에 컨텍스트 정보가 부족합니다.

그러나 문제가 생겼습니다. 컴파일러는 완전히 이해하지 못하는 오류를 생성하며, 내 연구 중 어떤 것도 밖으로 패닝하지 않습니다. 오류 : http://bytes.com/topic/c/answers/138897-error-insufficient-contextual-information-determine-type 나에게 문제가 클래스 선언에 있음을 좁혀 도움 :

부족 상황에 맞는 정보가 내가 인터넷에서 찾을 수있는 가장 가까운이 페이지이었다 유형을

을 결정. 그러나 여기에 제공된 솔루션은 클래스 생성자가 매개 변수를 사용하기 때문에 정확하게 적용되지 않습니다. 이전에 질문 한 질문은 내 상황에도 적용되지 않는 것 같습니다. 파일 출력과 템플릿 중 어느 것도 사용하지 않았기 때문입니다.

아래에 내 프로그램의 큰 덩어리를 올려 놓았습니다. 오류를 생성하는 부분은 쉽게 찾을 수 있도록 주석 달린 별표로 표시했습니다. 그러나, 나는 그 밖의 다른 곳에서 코드의 나머지 부분을 많이 남겼습니다.

헤드 업 : alObject.paint (255,255,255)와 같이 알레그로 5에서 인식하지 못하는 코드가있을 수 있습니다. 저에게 알레그로 객체와 함수 중 일부를 자신의 클래스로 통합하여 컴파일러가 오류를 생성하지 않기 때문에 여기에 포함시키지 않은 소스를 조금 더 쉽게 만들 수 있습니다.

#include <allegro5/allegro.h> 
#include <allegro5/allegro_primitives.h> 
#include "allegro.h" 

struct CBox{ 
    CBox(int _x, int _y, int _w, int _h):x(_x),y(_y),w(_w),h(_h){} 

    CBox(const CBox& other){ 
    x = other.x; 
    y = other.y; 
    w = other.w; 
    h = other.h; 
    } 
    bool collides(const CBox& other){ 
    return not(other.x + other.w < x or other.y + other.h < y or other.x > x + y or other.y > y + h); 
    } 
    int x; 
    int y; 
    int w; 
    int h; 
}; 

class CPlayer{ 
private: 
    int score; 
    CBox box; 
    ALLEGRO_COLOR color; 
    double mov_y; 
    void testBounds(CBox&); 
public: 
    CPlayer(CBox p, ALLEGRO_COLOR col):box(p),color(col){mov_y = 0.0;} 
    void setScore(int new_s){score=new_s;} 
    int getScore(){return score;} 

    void setBox(const CBox& b){box=b;} 
    CBox getBox(){return box;} 

    void setXYMovement(double new_my){mov_y=new_my;} 
    double getXYMovement(){return mov_y;} 

    void move(CBox& bounds); 

    void draw(){ 
    al_draw_filled_rectangle(box.x, box.y, box.x + box.w, box.y + box.h, color); 
    } 
}; 

class CBall{ 
private: 
    CBox box; 
    ALLEGRO_COLOR color; 
    double mov_y; 
    double mov_x; 
    int last_touch; 
    void testCollision(CBox&, const CBox&, CPlayer*); 
    int testBounds(CBox&, const CBox&); 
public: 
    CBall(CBox p, ALLEGRO_COLOR col):box(p),color(col),last_touch(3){} 
    void setXYMovement(double new_mx, double new_my){ 
    mov_x = new_mx; 
    mov_y = new_my; 
    } 
    void move(const CBox& bounds, CPlayer* plys); 
    void draw(){ 
    al_draw_filled_circle(box.x + box.w/2, box.y + box.h/2, box.w/2, color); 
    } 
}; 

class GameLoop{ 
private: 
    CBox fieldbox(int, int, int, int); 

/************************************************/ 
/***********ERROR HERE?? ERROR HERE??************/ 
/************************************************/ 
    CBall ball(const CBox&, ALLEGRO_COLOR); 
    CPlayer player1(const CBox&, ALLEGRO_COLOR); 
    CPlayer player2(const CBox&, ALLEGRO_COLOR); 
/*************************************************/ 
/*************************************************/ 

public: 
    GameLoop(Allegro&); 
    void GameStart(Allegro&); 
    void runTimerChecks(ALLEGRO_EVENT&, Allegro&); 
    void runExit(ALLEGRO_EVENT&, Allegro&, bool&); 
    void playerInput(ALLEGRO_EVENT&, bool&); 
    void endPlayerInput(ALLEGRO_EVENT&); 
}; 

void CPlayer::move(CBox& bounds){ 
    //make sure the player doesn't go off-bounds 
    testBounds(bounds); 
    box.y+=(int)mov_y; 
    //Players can't move horizontally, so no bounds checking in that matter 
} 

void CPlayer::testBounds(CBox& bounds){ 
    if((mov_y < 0) && (box.y + mov_y < bounds.y)){ 
    box.y = bounds.y; 
    mov_y = 0; 
    } 
    else if((mov_y > 0) && (box.y + box.h > bounds.y + bounds.h)){ 
    box.y = bounds.y + bounds.h - box.h; 
    mov_y = 0; 
    } 
} 

//ghostbox is the precalculated ball's trajectory 
void CBall::move(const CBox& bounds, CPlayer* plys){ 
    CBox ghostbox(box.x+(int)mov_y, box.y+(int)mov_y, box.w, box.h); 

    // test collision for box players 
    testCollision(ghostbox, bounds, plys); 
    testBounds(ghostbox, bounds); 
} 

void CBall::testCollision(CBox& ghostbox, const CBox& bounds, CPlayer* plys){ 
    for(int i = 0; i < 2; i++){ 
    //a player cannot touch the ball twice in a row 
    if(i != last_touch){ 
     CBox other = plys[i].getBox(); 
     if(ghostbox.collides(other)){ 
     //set the last touch to this player 
     last_touch = i; 
     //negate the "ghost movement" in x axis 
     ghostbox.x -= (int)mov_x; 
     //bounce horizontally 
     mov_x = -mov_x; 
     //bounce vertically to change the ball's trajectory 
     mov_y = (((box.y+box.h/2.0)-(other.y+other.h/2.0))/other.h)*10; 
     break; 
     } 
    } 
    } 
} 

int CBall::testBounds(CBox& ghostbox, const CBox& bounds){ 
    if(ghostbox.y < bounds.y){ 
    ghostbox.y = bounds.y; 
    mov_y = -mov_y; 
    } 
    else if(ghostbox.y + ghostbox.h > bounds.y + bounds.h){ 
    ghostbox.y = (bounds.y + bounds.h - ghostbox.h); 
    mov_y = -mov_y; 
    } 

    if(ghostbox.x + ghostbox.w < bounds.x){ 
    box.x = bounds.x + bounds.w/2 - bounds.w/2; 
    box.y = bounds.y + bounds.h/2 - bounds.h/2; 
    return 2; 
    } 
    else if(ghostbox.x > bounds.x + bounds.w){ 
    box.x = bounds.x + bounds.w/2 - box.w/2; 
    box.y = bounds.y + bounds.h/2 - box.h/2; 
    return 1; 
    } 
    box = ghostbox; 
    return 0; 
} 

GameLoop::GameLoop(Allegro& alObject){ 
    // This box is our playfield (covers the whole screen) 
    fieldbox(0,0,alObject.getWidth(), alObject.getHeight()); 
    //we setup the ball at the center of the screen with a white color 
    ball(CBox(alObject.getWidth()/2-10,alObject.getHeight()/2-10,20,20),alObject.paint(255,255,255)); 

    //red player on the left 
    player1(CBox(10,alObject.getHeight()/2-80/2,20,80), alObject.paint(255,0,0)); 
    //blue player on the right 
    player2(CBox(alObject.getWidth()-10-20,alObject.getHeight()/2-80/2,20,80), alObject.paint(0,0,255)); 
} 

void GameLoop::GameStart(Allegro& alObject){ 
    /* 
    when this variable is set to true the program will quit the main loop 
    and free the allocated resources before quitting. 
    */ 

    bool exit = false; 

    //we tell the ball to move to the left 

/***********************************************/ 
/***********************************************/ 
    ball.setXYMovement(-5.0,5.0); // GENERATES THE ERROR 
/***********************************************/ 
/***********************************************/ 

    while(!exit){ 
    al_wait_for_event(alObject.eventq, NULL); 
    while(al_get_next_event(alObject.eventq, &alObject.event)){ 
     if(alObject.event.type == ALLEGRO_EVENT_TIMER){ 
     runTimerChecks(alObject.event, alObject); 
     } 
     else if(alObject.event.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ 
     // quit if the user tries to close the window 
     runExit(alObject.event, alObject, exit); 
     } 
     else if(alObject.event.type == ALLEGRO_EVENT_KEY_DOWN){ 
     playerInput(alObject.event, exit); 
     } 
     else if(alObject.event.type == ALLEGRO_EVENT_KEY_UP){ 
     endPlayerInput(alObject.event); 
     } 
    } 
    } 
} 

void GameLoop::runTimerChecks(ALLEGRO_EVENT& event, Allegro& alObject){ 
    if(alObject.event.timer.source == alObject.getTimer()){ 
    //fill the screen with black 
    al_clear_to_color(alObject.paint(0,0,0)); 
    //move and draw our two players 
/**************************************************/ 
/**************************************************/ 
    player1.move(fieldbox); // GENERATES THE ERROR 
    player1.draw();   // GENERATES THE ERROR 
    player2.move(fieldbox); // GENERATES THE ERROR 
    player2.draw();   // GENERATES THE ERROR 
/**************************************************/ 
/**************************************************/ 
    } 
} 

void GameLoop::runExit(ALLEGRO_EVENT& event, Allegro& alObject, bool& exit){ 
    if(event.display.source == alObject.getDisplay()){ 
    exit = true; 
    } 
} 
void GameLoop::playerInput(ALLEGRO_EVENT& event, bool& exit){} 
void GameLoop::endPlayerInput(ALLEGRO_EVENT& event){} 

답변

0

예, 오류가 여기에 있습니다 : 당신이 (: player1.draw();과 같은 코드에 따라)하지 생각하는이, ball, player1player2라는 회원를 선언하지 않는

CBall ball(const CBox&, ALLEGRO_COLOR); 
CPlayer player1(const CBox&, ALLEGRO_COLOR); 
CPlayer player2(const CBox&, ALLEGRO_COLOR); 

. 대신, 여러분이 작성한 것은 메소드의 선언이며 지정한 매개 변수를 인수로 취합니다. 대신 수행해야합니다

CBall ball; 
CPlayer player1; 
CPlayer player2; 

을 다음 GameLoop의 생성자에서, 당신이 원하는대로 값을 초기화 initialization lists를 사용하여 :

GameLoop::GameLoop(Allegro& alObject) : 
    ball(/* ... */), 
    player1(/* ... */), 
    player2(/* ... */) 
{ 
    // .... 
} 
+0

감사 잔뜩! 그것은 그것을 고쳤다. 귀찮게해서 미안해. – Riverwolf

+0

@ 리버 울프 당신은 환영합니다 :-) 만약 당신의 문제를 해결했다면 대답을 받아 들여야합니다. 약 1 분 안에 StackOverflow가 어떻게 작동하는지보실 수 있습니다 :-) – Boris