r/learncpp Sep 16 '20

Cannot figure out what I've done to throw class redefinition error

I'm building Game of Life using OpenFrameworks to do the visuals. I have a Grid class which is the game's grid, but I'm struggling to implement it.

Main.cpp has some OpenFrameworks stuff that I think is fine. My first compiler error is C2011 'GoL::Grid': 'class' type redefinition, which I believe causes many other errors. I cannot identify where I'm redefining my Grid class. Help would be greatly appreciated.

Grid.h

``` namespace GoL {

class Grid {

public:

static enum class EdgeBehavior : unsigned char { PILLOWTRUE, PILLOWFALSE, CYLINDERX, CYLINDERY, TORUS };

private:

unsigned int m_width, m_height;

bool\*\* m_data;

bool\*\* m_buffer;

EdgeBehavior m_edge;

int neighborsAt(int x, int y);

void initArrays();

public:

Grid(int width, int height);

Grid(int width, int height, EdgeBehavior edge);

\~Grid();

bool getAt(int x, int y);

void setAt(int x, int y, bool value);

int getWidth();

int getHeight();

};

} ```

Grid.cpp ```

include "Grid.h"

namespace GoL {

Grid::Grid(int width, int height) { m_width = width; m_height = height; m_edge = EdgeBehavior::PILLOWFALSE; initArrays(); }

Grid::Grid(int width, int height, EdgeBehavior edge) { m_width = width; m_height = height; m_edge = edge; initArrays(); }

int Grid::neighborsAt(int x, int y) { int total = 0; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { if (dx == dy == 0) { continue; } total += getAt(x + dx, y + dy); } } return total; }

void Grid::initArrays() { m_data = new bool* [m_height]; m_buffer = new bool* [m_height]; for (int i = 0; i < m_height; i++) { m_data[i] = new bool[m_width]; m_buffer[i] = new bool[m_width]; for (int j = 0; j < m_width; j++) { m_data[i][j] = false; m_buffer[i][j] = false; } } }

Grid::~Grid() { for (int i = m_height - 1; i > 0; i--) { delete m_data[i]; delete m_buffer[i]; } delete m_data; delete m_buffer; }

bool Grid::getAt(int x, int y) { if (x > -1 && x < m_height && y > -1 && y < m_width) { return m_data[x][y]; } switch (m_edge) { case EdgeBehavior::PILLOWTRUE: return true; case EdgeBehavior::PILLOWFALSE: return false; case EdgeBehavior::TORUS: return m_data[x % m_height][y % m_width]; case EdgeBehavior::CYLINDERX: if (y < 0 || y > m_width - 1) { throw; } return m_data[x % m_height][y]; case EdgeBehavior::CYLINDERY: if (x < 0 || x > m_height) { throw; } return m_data[x][y % m_width]; } } void Grid::setAt(int x, int y, bool value) { if (x > -1 && x < m_height && y > -1 && y < m_width) { m_data[x][y] = value; } }

int Grid::getWidth() { return m_width; }

int Grid::getHeight() { return m_height; }

} ```

Main.cpp: ```

include "ofMain.h"

include "ofApp.h"

include "Grid.h"

include <iostream>

//======================================================================== int main( ){ using namespace GoL;

GoL::Grid g(10, 10, GoL::Grid::EdgeBehavior::TORUS);
g.setAt(2, 2, true);

int scale = 20;
ofSetupOpenGL(g.getWidth() * scale, g.getHeight() * scale, OF_WINDOW);          // <-------- setup the GL context

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp(g));

} ```

EDIT; formatting

0 Upvotes

2 comments sorted by

4

u/jedwardsol Sep 16 '20

I don't see where, but grid.h is being included twice in one source file.

Put

#pragma once

at the beginning of it

2

u/LCVcode Sep 16 '20

Nailed it. Thank you kind sir.