c++ - 绘制扫雷游戏板时遇到问题
问题描述
我正在使用 C++ 构建扫雷游戏。现在我迷失了试图读取输入文件并使用内容来构建我的游戏板看起来像这样: GameBoard
这是我到目前为止所拥有的:
主文件
#include <iostream>
#include <string>
#include "UI.hpp"
#include "drawboard.hpp"
int main()
{
UI start;
start.Gameprompt();
int drawgame[4][4] = {{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '}};
drawBoard(drawgame);
return 0;
}
用户界面.hpp
#ifndef UI_HPP
#define UI_HPP
#include <iostream>
#include <fstream>
#include <string>
class UI
{
private:
std::string filename;
public:
void GamePrompt();
};
#endif
用户界面.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());
}
}
画板.hpp
#ifndef DRAWBOARD_HPP
#define DRAWBOARD_HPP
class drawBoard
{
private:
int board[4][4];
public:
drawBoard(int board[][4]);
};
#endif
画板.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]
预先感谢您的任何帮助
解决方案
这个错误实际上有点晦涩......
当编译器看到该行时
drawBoard(drawgame);
它认为你将变量定义drawgame
为一个drawBoard
实例,即它认为你正在做什么
drawBoard drawgame;
解决方案很简单,像往常一样定义变量,然后将数组传递给构造函数,如
drawBoard board(drawgame);
或者如评论中所述,您可以这样做
drawBoard{drawgame};
但这只会创建一个将立即销毁的临时对象。 drawBoard
推荐阅读
- nginx - 精确路径限制Kong速率
- postgis - Imposm:从标签属性导入值
- json - Ansible 循环 JSON 数组包含对象并替换文件中 JSON 中找到的每个元素
- azure - 未找到 Terraform Azure App Gateway 后端 http 设置
- jenkins - 如何在 Jenkins Build 命令中使用变量
- .net-core - 安装时 nuget 包中缺少静态文件
- objective-c - 如何将 FlutterStandardTypedData 转换为 NSArray?
- c# - 在 using 语句中决定是否将参数传递给方法
- django - Django 双嵌套序列化器
- count - 使用 Ansible 查找进程数