2016-10-23 9 views
1

私はConwayのGame of Lifeを作りたいと思います。私は2つのクラスのセルとボードがあり、ボードでは2Dのダイナミックアレイタイプのセルを作成したいと思います。この配列をCellのフィールドにどうやって渡すかわからない。コンパイラshow2Dオブジェクトの動的配列のフィールドに移動する方法は?

C:\Users\Ja\Desktop\ObjectProject\ObjectGameOfLife\board.cpp:54: error: C2248: 'Cell::state_current': cannot access private member declared in class 'Cell'

私の英語のために残念です。

#ifndef CELL_H 
#define CELL_H 

#include <iostream> 
#include <cstdlib> 

using namespace std; 

class Cell 
{ 
    bool state_current; 
    bool state_future; 
    int neighbors; 

    void editCell(bool n_state); 

public: 
    Cell(); 
    void show(); 
    void edit(); 
}; 

cell.cpp

#endif // CELL_H 
#include "cell.h" 

Cell::Cell() 
{ 
    int a=0; 

    a=rand()%2; 
    if(a==1) 
    state_current=true; 
    else 
    state_current=false; 

    state_future=false; 
    neighbors=0; 
} 

void Cell::show() 
{ 
    if(state_current==true) 
    cout<<'X'; 
    else 
    cout<<'O'; 
} 

void Cell::editCell(bool n_state) 
{ 
    state_current=n_state; 
} 

void Cell::edit() 
{ 
    int option; 

    cout<<"choose avaible option:\n0.dead\n1.alive"<<endl; 

    cin>>option; 
    while(option!=1 && option!=0) 
    { 
    cout<<"choose avaible option"<<endl; 
    cin>>option; 
} 

    if(option==1) 
    editCell(true); 
    else 
    editCell(false); 
} 

board.h

#ifndef BOARD_H 
#define BOARD_H 

#include "cell.h" 

class Board 
{ 
    int v; 
    int c; 
    Cell **t; 
public: 
    Board(int a=10, int b=10); //konstruktor z wartościami domyślnymi 
    void showBoard(); 
    void getSize(); 
    void createBoard(); 
    void checkNeighborhood(int x, int y); 
    void rules(int x, int y); 
    void nextGen(); 
}; 

#endif // BOARD_H 

board.cpp

#include "board.h" 

Board::Board(int a,int b) 
{ 
    v=a; 
    c=b; 
    t=new Cell *[v]; 

    for(int i=0; i<v; i++) 
     t[i]=new Cell [c]; 
} 

void Board::createBoard() 
{ 
    t=new Cell *[v]; 

    for(int i=0; i<v; i++) 
     t[i]=new Cell [c]; 
} 

void Board::showBoard() 
{ 
    for(int i=0; i<v; i++) 
    { 
     for(int j=0; j<c; j++) 
      t[i][j].show(); 
     cout<<endl; 
    } 
} 

void Board::getSize() 
{ 
    int a,b; 

    cout<<"Enter natural numbers"<<endl; 
    cin>>a; 
    cin>>b; 
    while(a<1 && b<1) 
    { 
     cout<<"Board can't have this size. Enter natural numbers"<<endl; 
     cin>>a; 
     cin>>b; 
    } 
    v=a; 
    c=b; 
} 

void Board::checkNeighborhood(int x, int y) 
{ 
    for(int i=x-1; i<x+2; i++) 
     for(int j=y-1; j<y+2; j++) 
      if(i>=0 && i<v && j>=0 && j<c) 
       if(!(i==x && j==y)) 
        if(t[i][j].state_current==true)//first crash 
         t[i][j].neighbors++; 
} 

void Board::rules(int x, int y) 
{ 

    if(t[x][y].state_current==true) 
     if(t[x][y].neighbors<2 || t[x][y].neighbors) 
      t[x][y].state_future=false; 
     else 
      t[x][y].state_future=true; 
    else 
     if(t[x][y].neighbors==3) 
      t[x][y].state_future=true; 
     else 
      t[x][y].state_future=false; 
} 

void Board::nextGen() 
{ 
    for(int i=0; i<v; i++) 
     for(int j=0; j<c; j++) 
     { 
      rules(i,j); 
      t[i][j].state_current=t[i][j].state_future; 
     } 
} 

答えて

0

問題は、それがあることを述べているまさにです。別のオブジェクトからプライベート属性を変更しようとしています。この行で:あなたは右のオブジェクトに到達し、むしろその状態が何であるかのセルを尋ねるよりも、属性(メンバ変数)のホールドをつかむしようとし

 if(t[i][j].state_current==true)//first crash 

注意してください。

機能をカプセル化して非公開にする目的の1つは、他のオブジェクトが内部に到達せず、物事を自由に変更することを防ぐことです。単純にここでチェックを行っていますが、適切な方法は、オブジェクトメソッドを使用してオブジェクトに問い合わせることです。公共の関数としてごCell.hとCell.cppにこのような何かを追加します。

boolean Cell::status() 
{ 
    return state_current; 
} 

そして、そのようにそれを呼び出す:

明らか
if(t[i][j].status()==true) 

、あなたがに同じオブジェクト指向のアプローチを取る必要があります残りの操作は、次にあなたが細胞に行う操作です。セルを直接変更したり、オブジェクトの変更方法を伝えたり、セルでセルを管理させたりしないでください。これにより、コードを再利用して読みやすくなります。

+0

この問題を解決する最も簡単な方法は、Cellのsetメソッドとgetメソッドを作成して使用することです。 – Bukaj25

+0

はい。短期的には、伝統的なCのアプローチ(命令的スタイル)より多くのコードを必要とします。あなたが手に入りたいものを手に入れますが、長期的には、プロジェクトの規模が大きくなるにつれて、これは絶対に正しい、アプローチ。 –

+0

Cellが持つすべての動作のためのメンバ関数を作成し、Cellに要求したいすべてのものについて作成します。その後、それらを使用します。セルを不透明にします。より大きいプロジェクトで* this *コードを再度使用することはできませんが、オブジェクトへのインタフェースをうまく定義していれば、他のプロジェクトに簡単に持ち上げてドロップすることができるコードを作成することができます。 –

関連する問題