I am working on building a Minesweeper game using C++. Right now I am lost trying to read in an input file and use the contents to build my game board to look like this: GameBoard
This is what I have so far:
main.cpp
#include <iostream>
#include <string>
#include "UI.hpp"
#include "drawboard.hpp"
int main()
{
UI start;
start.Gameprompt();
int drawgame[4][4] = {{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '}};
drawBoard(drawgame);
return 0;
}
UI.hpp
#ifndef UI_HPP
#define UI_HPP
#include <iostream>
#include <fstream>
#include <string>
class UI
{
private:
std::string filename;
public:
void GamePrompt();
};
#endif
UI.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "UI.hpp"
void UI::GamePrompt()
{
std::ifstream inFS;
while (!inFS.is_open())
{
std::cout << "Please enter a file name with the minefield information: " << std::endl;
std::cin >> filename;
inFS.open(filename.c_str());
}
}
drawboard.hpp
#ifndef DRAWBOARD_HPP
#define DRAWBOARD_HPP
class drawBoard
{
private:
int board[4][4];
public:
drawBoard(int board[][4]);
};
#endif
drawboard.cpp
#include "drawboard.hpp"
#include<iostream>
drawBoard::drawBoard(int board[][4])
{
std::cout << " 0 1 2 3 " << std::endl;
for(int i = 0; i < 4; i++)
{
std::cout << " +---+---+---+---+" << std::endl;
std::cout << i + 1;
for(int j = 0; j < 4; j++)
{
std::cout << " | " << board[i][j];
}
std::cout << " | " << std::endl;
}
std::cout << " +---+---+---+---+" << std::endl;
}
These are the errors that I am receiving right now:
main.cpp: In function ‘int main()’:
main.cpp:18:20: error: conflicting declaration ‘drawBoard drawgame’
drawBoard(drawgame);
^
main.cpp:16:6: error: ‘drawgame’ has a previous declaration as ‘int drawgame [4][4]’
int drawgame[4][4] = {{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '}};
^
main.cpp:16:6: warning: unused variable ‘drawgame’ [-Wunused-variable]
Thank you in advance for any help
This error is actually kind of obscure...
When the compiler sees the line
drawBoard(drawgame);
it thinks you're defining the variable drawgame
as a drawBoard
instance, i.e. what it thinks is that you're doing
drawBoard drawgame;
The solution is simple, define the variable like you normally would, and then pass the array to the constructor, as in
drawBoard board(drawgame);
Or as mentioned in a comment you could do
drawBoard{drawgame};
But this only creates a temporary drawBoard
object that will be destructed immediately.