這是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_ */
您缺少一些'class'聲明...並指定* exact *錯誤。 – crashmstr
你已經發布了大量不相關的代碼,但沒有聲明'rep','Cell'的定義或錯誤消息。沒有這些信息很難猜測出現了什麼問題。 –
我的問題是,如何初始化rep [i] [j],初始化setAlive(bool b) – mohsen