error C2011: 'Game': 'struct' type redefinition в чем причина?
//Initializer.cpp
#include "Game.h"
#include "time.h"
#include <iostream>
int main()
{
srand((int)time(NULL));
Game game;
game.CreateBoard();
return 0;
}
//Game.h
#pragma once
#include "Cell.h"
#define coordXmax 10 //минимально по координате X
#define coordYmax 10 //минимально по координате Y
#define minAmountBomb 10 // минимально бомб
struct Game
{
void CreateBoard()
{
}
//void ShowBoard();
};
//Game.cpp
#include "Game.h"
#include "Cell.h"
struct Game
{
void CreateBoard()
{
int counter = 0;
Cell *board = new Cell[coordXmax * coordYmax]{};
for (int i = 1; i <= coordYmax; ++i)
{
for (int j = 1; j <= coordXmax; ++j)
{
Cell cell;
cell.pos.coordY = i;
cell.pos.coordX = j;
cell.countBomb = 0;
cell.symbol = '#';
cell.isBoomb = false;
cell.isOpen = false;
board[counter++] = cell;
}
}
}
void ShowBoard()
{
}
};
//Position.h
#pragma once
struct Position
{
int coordX;
int coordY;
};
//Cell.h
#pragma once
#include "Position.h"
struct Cell
{
Position pos;
int countBomb;
bool isBoomb;
bool isOpen;
char symbol;
};
1>------ Build started: Project: Miner_ver_004, Configuration: Debug Win32 ------
1> Initializer.cpp
1> Game.cpp
1>c:\users\mazur\documents\visual studio 2015\projects\miner_ver_004\miner_ver_004\game.cpp(5): error C2011: 'Game': 'struct' type redefinition
1> c:\users\mazur\documents\visual studio 2015\projects\miner_ver_004\miner_ver_004\game.h(9): note: see declaration of 'Game'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
У вас в game.h есть структура
struct Game
{
void CreateBoard()
{
}
//void ShowBoard();
};
Но структура с тем же именем у вас есть и в game.cpp!
struct Game
{
void CreateBoard()
{
int counter = 0;
Cell *board = new Cell[coordXmax * coordYmax]{};
Структуры/классы - это не функции, тут не нужно давать "пустое" объявление, а потом определение.
Если уж хотите вынести реализацию в .cpp-файл, то в game.h оставьте объявление структуры
struct Game
{
void CreateBoard();
void ShowBoard();
};
а в game.cpp поместите реализацию ее членов.
void Game::CreateBoard()
{
int counter = 0;
Cell *board = new Cell[coordXmax * coordYmax]{};
for (int i = 1; i <= coordYmax; ++i)
{
for (int j = 1; j <= coordXmax; ++j)
{
Cell cell;
cell.pos.coordY = i;
cell.pos.coordX = j;
cell.countBomb = 0;
cell.symbol = '#';
cell.isBoomb = false;
cell.isOpen = false;
board[counter++] = cell;
}
}
}
void Game::ShowBoard()
{
}
Продвижение своими сайтами как стратегия роста и независимости