2010-01-31 89 views
0

我創建了一個類,它編譯時沒有語法錯誤,但是我得到6個未解析的外部符號?獲取未解析的外部錯誤

類:

struct CELL { 
private: 
    static bool haslife; 
static int x; 
static int y; 
public: 

    static bool has_life() 
    { 
     return haslife; 
    } 

    static void set_coords(int xcoord, int ycoord) 
    { 
     x = xcoord; 
     y = ycoord; 
    } 
    static void get_coords(int &xcoord, int &ycoord) 
    { 
     xcoord = x; 
     ycoord = y; 
    } 


}; 


class cell_grid { 
private: 
static int cell_size; 
static int cell_count_x; 
static int cell_count_y; 
CELL **cell; 
public: 
    cell_grid(); 
    cell_grid(unsigned int width,unsigned int height, unsigned int cellsize) 
    { 

     //set size based on cellsize 

     this->cell_size = cellsize; 
     this->cell_count_x = floor((double)width/this->cell_size); 
     this->cell_count_y = floor((double)height/this->cell_size); 


     this->cell = new CELL*[this->cell_count_y]; 

     for(int i = 0; i < this->cell_count_y; i++) 
     { 
      cell[i] = new CELL[this->cell_count_x]; 
     } 

     for(int y = 0; y < this->cell_count_y; ++y) 
     { 
      for(int x = 0; x < this->cell_count_x; ++x) 
      { 
       int cur_x = x * this->cell_size; 
       int cur_y = y * this->cell_size; 
       this->cell[x][y].set_coords(cur_x,cur_y); 
      } 
     } 

    } //end of constructor 

    static int get_cell_size() 
    { 
     return cell_size; 
    } 
static void render(BITMAP *buff) 
{ 
    circlefill(buff,70,70,60,makecol(27,37,0)); 

} 


}; 

主要

int main() 
{ 
    start_allegro(); 
    cell_grid *grid = new cell_grid(scr_w,scr_h,10); 
    grid->render(buffer); 


     //Main Loop 
    while (!done && !key[KEY_ESC]) //until 'X' pressed or ESC 
{ 
//***** Start Main Code Here ***** 
    while (speed_counter > 0) 
    { 



       //render the buffer to the screen 

      blit(
      buffer, 
      screen, 
      0,0,0,0, 
      scr_w, 
      scr_h); 

      clear_bitmap(buffer); 

     speed_counter --; 
    } 
//***** End Main Code Here ***** 
rest(1); //Normalize cpu usage 
} 
    return 0; 
} 
END_OF_MAIN() 

感謝

+0

什麼是無法解決的符號? – 2010-01-31 16:02:45

回答

4

並沒有規定所有的類變量的靜態。
將數據成員定義爲靜態時,意味着它只有一個單一實例。這似乎不是你想要在這裏做的。
而不是

private: 
    static bool haslife; 
    static int x; 
    static int y; 

寫:

private: 
    bool haslife; 
    int x; 
    int y; 

更進一步,當你定義一個靜態成員,您需要在CPP文件中再次定義,並用一個值初始化。它看起來不像你這樣做,這就是爲什麼你得到鏈接器錯誤。

另外,下次您發佈內容時,請確保您實際提出問題,而不僅僅是陳述事實。

+0

好點,看起來不像是靜態的。 – 2010-01-31 16:06:28

+0

是的。缺省的cell_grid構造函數也缺失。 – 2010-01-31 16:08:37

+0

謝謝,我讓所有東西都是靜態的,因爲在過去,我的編譯器在困擾着我時,如果他們願意,但是當你創建類實例時,它是不必要的。 – jmasterx 2010-01-31 16:26:46