首页 > 解决方案 > 绘制扫雷游戏板时遇到问题

问题描述

我正在使用 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]

预先感谢您的任何帮助

标签: c++data-structuresminesweeper

解决方案


这个错误实际上有点晦涩......

当编译器看到该行时

drawBoard(drawgame);

它认为你将变量定义drawgame为一个drawBoard实例,即它认为你正在做什么

drawBoard drawgame;

解决方案很简单,像往常一样定义变量,然后将数组传递给构造函数,如

drawBoard board(drawgame);

或者如评论中所述,您可以这样做

drawBoard{drawgame};

但这只会创建一个将立即销毁的临时对象。 drawBoard


推荐阅读