3

I am trying to implement a simple version of Conway's Game of Life which consists of a header file and three .cpp files (two for class functions, one for main). Here I have included my header files and two class function declaration files ( the compiler is having no problem with my Main.cpp file).

Game_Of_Life.h

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std; 

class cell{
    public:
        cell(); 
        int Current_State();                        // Returns state (1 or 0)
        void Count_Neighbours(cell* A);             // Counts the number of living cells in proximity w/o wraparound
        void Set_Future();                          // Determines the future value of state from # of neighbbours
        void Update();                              // Sets state to value of future state
        void Set_Pos(unsigned int x, unsigned int y);   // Sets position of cell in the array for use in counting neighbours 
    private:
        int state; 
        int neighbours; 
        int future_state; 
        int pos_x; 
        int pos_y; 
}; 

class cell_array{
    public:
        cell_array();                                   
        void Print_Array();                             // Prints out the array
        void Update_Array();                            // Updates the entire array
        void Set_Future_Array();                        // Sets the value of the future array
    private:
        cell** A; 
}; 

Cell_Class_Functions.cpp

#include "Game_Of_Life.h"

cell::cell(){
    state = rand() % 2; 
    return; 
}

void cell::Set_Future (){
    if (state == 1){
        if (neighbours < 2) future_state = 0; 
        else if (neighbours == 2 || neighbours == 3) future_state = 1; 
        else if (neighbours > 3) future_state = 0; 
    }
    else{
        if (neighbours == 3) future_state = 1; 
    }
    return; 
}

void cell::Update (){
    state = future_state; 
    return; 
}

int cell::Current_State (){
    return state; 
}

void cell::Set_Pos (unsigned int x, unsigned int y){
    pos_x = x; 
    pos_y = y; 
    return; 
}

void Count_Neighbours (cell* A){
    neighbours = 0; 
    if (pos_x > 0) neighbours += A[pos_y * 10 + pos_x - 1].Current_State(); 
    if (pos_x < 9) neighbours += A[pos_y * 10 + pos_x + 1].Current_State(); 
    if (pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x].Current_State(); 
    if (pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x].Current_State(); 
    if (pos_x > 0 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x - 1].Current_State(); 
    if (pos_x > 0 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x - 1].Current_State(); 
    if (pos_x < 9 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x + 1].Current_State(); 
    if (pos_x < 9 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x + 1].Current_State(); 
    return; 
}

Cell_Array_Class_Functions.cpp

#include "Game_Of_Life.h"

cell_array::cell_array(){
    A = (cell**) malloc (sizeof(cell*)*100); 
    for (unsigned int r = 0; r < 10; r++){
        for (unsigned int c = 0; c < 10; c++){
            *A[r * 10 + c].Set_Pos(r,c); 
        }
    }
    return; 
}

void cell_array::Update_Array(){
    for (unsigned int r = 0; r < 10; r++){
        for (unsigned int c = 0; c < 10; c++){
            *A[r * 10 + c].Update(); 
        }
    }
}

void cell_array::Set_Future_Array(){
    for (unsigned int r = 0; r < 10; r++){
        for (unsigned int c = 0; c < 10; c++){
            *A[r * 10 + c].Count_Neighbours(A);
            *A[r * 10 + c].Set_Future(); 
        }
    }
    return; 
}

void cell_array::Print_Array(){
    cout << "\n"; 
    for (unsigned int r = 0; r < 10; r++){
        for (unsigned int c = 0; c < 10; c++)cout << *A[r * 10 + c].Current_State() << " "; 
        cout << "\n"; 
    }
    return; 
}

As far as I understand, since I included the header file with the class declarations, then I should be able to access the private members of the class through the previously declared functions in the class.

Essentially the Error Report Looks Like

Error C2065 'item' : undeclared identifier

This error appears for every private member called from the cell class.

What am I doing wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Chubzorz
  • 135
  • 1
  • 2
  • 7
  • 2
    `Essentially the Error Report Looks Like` Essentially, the problem is somewhere in your code. Is this good enough of an answer? You certainly feel yours is good enough of a question. `Help Plz?` If you really wanted help, you would have shown the exact error message, and the line of code it points to. But you haven't, which suggests to me that you don't really want help. – Igor Tandetnik Sep 24 '15 at 04:38
  • Your use of `malloc` is incorrect. Malloc can only be used for PODs. Use `vector` instead. – M.M Sep 24 '15 at 06:18
  • @M.M but a pointer (or array thereof) *is* a POD. Doesn't make the following code any less UB-inducing ;) – Quentin Oct 23 '17 at 12:20

2 Answers2

2

Also, in your Cell_Array_Class_Functions.cpp you need to adjust your functions.

The . operator is used on objects and references.You have to deference it first to obtain a reference. That is:

(*A[r * 10 + c]).Set_Pos(r,c);

Alternatively, you can use (this is the preferred and easier to read way):

A[r * 10 + c]->Set_Pos(r,c);

The two are equivalent.

kemotoe
  • 1,730
  • 13
  • 27
  • 2
    The second is by far the better, more idiomatic C++. The first notation gets really clunky if you abuse the Law of Demeter enough to have `name1->ptr2->ptr3.member4->member5[index]`; that is a lot more readable than `(*(*(*name1).ptr2).ptr3.member4).member5[index]` (though it isn't very nice even with the arrow operator). – Jonathan Leffler Sep 24 '15 at 06:00
1

I don't see the word item anywhere in your code. However, you need to fix:

void Count_Neighbours (cell* A){ ... }

It should be:

void cell::Count_Neighbours (cell* A){ ... }
R Sahu
  • 204,454
  • 14
  • 159
  • 270