2013-08-24 31 views
1

情況:我想使用主類中聲明的變量中存儲的數字,但是我需要在另一個類中使用它們。我試圖把的const int變量在.h文件的#include主類和其他類然而這種錯誤,進來的values.h文件:如何從另一個類的主類使用全局const int變量

error C2370: 'LEVEL_HEIGHT' : redefiniton; different storage class 

.h文件中:

int SCREEN_WIDTH = 640; 
int SCREEN_HEIGHT = 480; 
int SCREEN_BPP = 32; 

//The frame rate 
int FRAMES_PER_SECOND = 20; 

//The dot dimensions 
int PLAYER_WIDTH = 20; 
int PLAYER_HEIGHT = 20; 

//The dimensions of the level 
const int LEVEL_WIDTH = 1280; 
const int LEVEL_HEIGHT = 960; 

main.cpp中:

#include "SDL.h" 
#include "Player.h" 
#include "Values.h" 
#include "LoadImage.h" 



//The surfaces 
SDL_Surface *background = NULL; 
SDL_Surface *screen = NULL; 

//The event structure 
SDL_Event event; 

Player player; 
LoadImage game; 


//The camera 
SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; 


bool init() 
{ 
    /****************/ 
    /*Initialisation*/ 
    /****************/ 

    //Start SDL 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) 
    { 
     return false; 
    } 

    //Set up Screen 
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); 

    if (screen == NULL) 
    { 
     return false; 
    } 
    //Set up window caption 
    SDL_WM_SetCaption("10 seconds", NULL); 
    return true; 
} 

void mainLoop() 
{ 
    bool quit = false; 
    //While the user hasnt quit 
    while (quit == false) 
    { 



     //Move the player 
     player.move(); 

     //Set the camera 
     player.setCamera(); 

     //Show the background 
     game.apply_surface(0,0,background, screen, &camera); 
     //Show the player on the screen 
     player.draw(); 


     //Update screen 
     SDL_Flip(screen); 
     /*if (SDL_Flip(screen) == -1) 
     { 
      return 1; 
     }*/ 


    } 
} 

void clean_up() 
{ 
//  SDL_FreeSurface(background); 
    player.clean(); 


     //Quit SDL 
     SDL_Quit(); 

} 

int main(int argc, char* args[]) 
{ 
    //Initialise SDL 
    if (init() == false) 
    { 
     return 1; 
    } 

    //player = new Player("player.png", screen); 

    //main loop 
    mainLoop(); 

    clean_up(); 
    return 0; 
} 

Player.cpp:

#include <iostream> 
#include <string> 
#include "Player.h" 
#include "Values.h" 


using namespace std; 

void Player::move() 
{ 
    //Move the player left or right 
    x += xVel; 

    //if the player went too far to the left or right 
    if ((x < 0)||(x + PLAYER_WIDTH > LEVEL_WIDTH)) 
    { 
     //move back 
     x -= xVel; 

    } 

    //Move player up or down 
    y += yVel; 

    //if the player went too far up or down 
    if ((y < 0)||(y + PLAYER_HEIGHT > LEVEL_HEIGHT)) 
    { 
     //move back 
     y -= yVel; 
    } 

} 

void Player::setCamera() 
{ 
    //Center the camera over the player 
    camera.x = (x + PLAYER_WIDTH/2) - SCREEN_WIDTH/2; 
    camera.y = (y + PLAYER_HEIGHT/2) - SCREEN_HEIGHT/2; 

    //Keep the camera in bounds 
    if (camera.x < 0) 
    { 
     camera.x = 0; 
    } 
    if (camera.y < 0) 
    { 
     camera.y = 0; 
    } 
    if (camera.x > LEVEL_WIDTH - camera.w) 
    { 
     camera.x = LEVEL_WIDTH - camera.w; 
    } 
    if (camera.y > LEVEL_HEIGHT - camera.h) 
    { 
     camera.y = LEVEL_HEIGHT - camera.h; 
    } 

} 

void Player::draw() 
{ 
    //Show the player 
    image.apply_surface(x - camera.x, y - camera.y, player, screen); 
} 

void Player::clean() 
{ 
    image.clean(); 
} 
+1

你有另外一個'LEVEL_HEIGHT'定義的地方不是const?不要在.h文件中定義這些變量,因爲它們不是常量。 – billz

+0

請顯示'Player.h'文件。 –

+0

修正了它,我只是忘了在values.h文件中添加Const到其他變量(屏幕寬度,fps等) –

回答

0

你可以用#pragma once開始你的標題。這可以防止您的標題被多次包含。另外,當您處理更大的項目時,請毫不猶豫地使用前向聲明來加快編譯時間。

相關問題