简体   繁体   中英

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".

This message comes up for line 9. I would really appreciate the help.

#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

#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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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