简体   繁体   English

错误:'board' 未在此范围内声明 (C++)

[英]Error: ‘board’ was not declared in this scope (C++)

I am writing code for a board game and trying to test it out.我正在为棋盘游戏编写代码并尝试对其进行测试。

I cannot figure out how to solve this error that gives the message:我不知道如何解决这个给出消息的错误:

error: 'board' was not declared in this scope".错误:'board' 未在此范围内声明”。

This message comes up for line 9. I would really appreciate the help.此消息出现在第 9 行。我非常感谢您的帮助。

#include <iostream>
#include "Board.h"

using namespace std;

int main()
{
    Board B;
    B.Player1(*board[15][15], B.pathP1[57]);
    B.printBoard();
}

void Board::Player1(char *board[15][15], char pathP1[57])
{ 

Board.h is below Board.h 在下面

#ifndef BOARD_H
#define BOARD_H

struct Board
{
  //Board board();
  char *board[15][15];
  char pathP1[57];
  
  void Player1(char *board[15][15], char pathP1[57]);
  void printBoard();
};
#endif //BOARD_H

So you have a misunderstanding about how objects work.所以你对对象的工作方式有误解。 There is no need to pass member variables to a member function because member functions have access to member variables automatically .不需要将成员变量传递给成员函数,因为成员函数可以自动访问成员变量。 That is one of the major features of object orientated programming (in C++ at least).这是面向对象编程的主要特征之一(至少在 C++ 中)。

Rewrite your code like this像这样重写你的代码

struct Board
{
  //Board board();
  char *board[15][15];
  char pathP1[57];
  
  void Player1();
  void printBoard();
};

int main()
{
    Board B;
    B.Player1();
    B.printBoard();
}

void Board::Player1()
{
    // do something with board and pathP1
    ...
}

In addition you need to revise how arrays and functions work.此外,您需要修改数组和函数的工作方式。 Although you don't need to pass the board and pathP1 arrays to the Player1 method the way you were trying to do it was also incorrect.虽然你不需要的传递boardpathP1阵列添加到Player1方法的方式,你试图做它也是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM