首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绘制扫雷游戏板有困难

绘制扫雷游戏板有困难
EN

Stack Overflow用户
提问于 2019-10-17 15:39:48
回答 1查看 129关注 0票数 1

我正在使用C++构建一个扫雷游戏。现在,我无法在输入文件中读取内容,并使用内容来构建我的游戏板,如下所示:GameBoard

到目前为止,这就是我所拥有的:

main.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#ifndef UI_HPP
#define UI_HPP


#include <iostream>
#include <fstream>
#include <string>

class UI
{

    private:
            std::string filename;


    public:
            void GamePrompt();


};


#endif

UI.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#ifndef DRAWBOARD_HPP
#define DRAWBOARD_HPP

class drawBoard
{

    private:
            int board[4][4];

    public:
            drawBoard(int board[][4]);


};

#endif

drawboard.cpp

代码语言:javascript
复制
#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;

}

以下是我现在所收到的错误:

代码语言:javascript
复制
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]

预先感谢您的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-17 15:46:21

这个错误其实有点模糊..。

当编译器看到行时

代码语言:javascript
复制
drawBoard(drawgame);

它认为您正在将变量drawgame定义为一个drawBoard实例,也就是说,它认为您正在执行的是

代码语言:javascript
复制
drawBoard drawgame;

解决方案很简单,像通常那样定义变量,然后将数组传递给构造函数,如下所示

代码语言:javascript
复制
drawBoard board(drawgame);

或者,正如评论中提到的,您可以这样做

代码语言:javascript
复制
drawBoard{drawgame};

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58436265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档