2013-11-25 3 views
-4

это мой Cell.cpp класс в C++:как использовать объект указателя класса в другом классе

#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] .Cell :: setAlive (истина); есть ошибка i dont know how initialize rep! пожалуйста, помогите мне

это 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' ... и укажите * точную * ошибку. – 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

Смежные вопросы