我正在使用C++构建一个扫雷游戏。现在,我无法在输入文件中读取内容,并使用内容来构建我的游戏板,如下所示:GameBoard
到目前为止,这就是我所拥有的:
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();
};
#endifUI.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]);
};
#endifdrawboard.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;
}以下是我现在所收到的错误:
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]预先感谢您的帮助
发布于 2019-10-17 15:46:21
这个错误其实有点模糊..。
当编译器看到行时
drawBoard(drawgame);它认为您正在将变量drawgame定义为一个drawBoard实例,也就是说,它认为您正在执行的是
drawBoard drawgame;解决方案很简单,像通常那样定义变量,然后将数组传递给构造函数,如下所示
drawBoard board(drawgame);或者,正如评论中提到的,您可以这样做
drawBoard{drawgame};但这只会创建一个将立即销毁的临时drawBoard对象。
https://stackoverflow.com/questions/58436265
复制相似问题