2013-11-25 43 views
-4

這是C++我Cell.cpp類:如何在另一個類使用類的指針對象

#include "Cell.h" 
Cell::Cell() { 
alive = true ; 
} 
void Cell::setAlive (bool b) { 
    Cell::alive = b ; 
} 

bool Cell::isAlive() { 
return Cell::alive ; 
} 

,這是我World.cpp類:

#include "World.h" 
#include "Cell.h" 
#include <ctime> 
#include <cstdlib> 
#include <iostream> 
using namespace std ; 
    World::World (int l , int c) : lines (l) , columns(c) , rep() {}; 
    World::World (int l , int c , bool ring) : lines (l) , columns(c) , ring (ring) ,rep() {}; 
    World::~World() {}; 

     int World::getLines() { 
      return lines; 
     } 
     int World::getColumns() { 
      return columns ; 
     } 
     void World::genarateWorld() { 

      srand (time(0)); 
       rep[1][1].Cell::setAlive(true); 


      } 
     } 

     int World::nbAliveNeighbor (int i , int j) { 
      int counter=0 ; 

      return counter ; 
     } 
     int World::nbAliveNeighborRing (int i , int j) { 
      int counter=0 ; 

      return counter ; 
     } 
     void World::nextGenarate() { 

     } 
     void World::print(){ 
      using namespace std ; 
      for (int i = 0 ; i < 5; i ++){ 
       for (int j = 0 ; j < 5 ; j++) { 
        if (rep [i][j].Cell::isAlive()) 
         my [i][j]='*'; 
        else 
         my [i][j]='-'; 
       } 

       for (int i = 0 ; i < 5; i ++){ 
        for (int j = 0 ; j < 5 ; j++) { 
         cout << my[i][j] << "\t" ; 
         } 
        cout <<endl ; 
      } 
     } 
     } 

,這是我的主:

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include "Cell.h" 
#include "World.h" 
using namespace std ; 

int main(){ 

    srand (time(0)); 
    World my (5,5); 

    my.genarateWorld(); 

// my.print(); 
    return 0 ; 
} 
我World.cpp在虛空世界:: genarateWorld()

代表[1] [1]。細胞:: setAlive(真);有一個錯誤 我不知道如何初始化代表! 請幫我

這是Cell.h

#ifndef CELL_H_ 
#define CELL_H_ 

class Cell { 
private: 
    bool alive ; 

public: 
    Cell() ; 
    void setAlive (bool b) ; 
    bool isAlive() ; 

}; 



#endif 

,這是World.h

#ifndef WORLD_H_ 
#define WORLD_H_ 

#include "Cell.h" 

class World { 
private : 
    bool ring ; 
    int lines , columns ; 
    Cell **rep ; 
    char my [5][5]; 

public: 
    World (int l , int c); 
    World (int l , int c , bool ring); 
    ~World() ; 
    int getLines() ; 
    int getColumns() ; 
    void genarateWorld() ; 
    int nbAliveNeighbor (int i , int j) ; 
    int nbAliveNeighborRing (int i , int j) ; 
    void nextGenarate() ; 
    void print(); 
}; 




#endif /* WORLD_H_ */ 
+0

您缺少一些'class'聲明...並指定* exact *錯誤。 – crashmstr

+0

你已經發布了大量不相關的代碼,但沒有聲明'rep','Cell'的定義或錯誤消息。沒有這些信息很難猜測出現了什麼問題。 –

+0

我的問題是,如何初始化rep [i] [j],初始化setAlive(bool b) – mohsen

回答

1

你添加在它不是需要各種各樣的地方的Cell::,和在一個地方它是一個錯誤

rep[1][1].setAlive(true); 

不是

rep[1][1].Cell::setAlive(true); 

bool Cell::isAlive() { 
    return alive ; 
} 

bool Cell::isAlive() { 
    return Cell::alive ; 
} 

void Cell::setAlive (bool b) { 
    alive = b ; 
} 

void Cell::setAlive (bool b) { 
    Cell::alive = b ; 
} 
+0

好,但這不是我的主要問題可能主要問題是rep! – mohsen

+0

@mohsen你需要更好地描述你的主要問題。我在上面的答案中談到了代表。使用'rep [1] [1] .setAlive(true);'。你的主要問題是別的嗎? – john

相關問題