c ++:OOP:如何在单独的头文件中更新数组?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c ++:OOP:如何在单独的头文件中更新数组?相关的知识,希望对你有一定的参考价值。
我是C ++的新手,正在尝试创建一个基本的迷宫游戏。目前,我正在尝试使玩家能够在迷宫中移动,其中玩家的移动功能会更新网格数组中的角色。网格数组位于Grid.h中,播放器位于Player.h中,我在每个文件中都包含了标头,但它们仍然无法访问彼此的成员变量。我尝试使用'extern',但这也没有用。
我的老师建议将m_grid数组发送给构造函数,但没有指定哪个构造函数,或者在任何情况下都没有指定如何执行。任何帮助将不胜感激,谢谢。
Grid.h
#include <iostream>
#include "Player.h"
class Grid
{
public:
Grid();
~Grid();
void Print_grid();
char m_grid[18][32] =
{
{"_______________________________"},
{"| _______ ____________ |"},
{" @ |"},
{"|_ _____________ __________|"},
{"| | |"},
{"| | |__ | | | | |"},
{"| | | | |___ | | | |"},
{"| | _____| | | ___| | |"},
{"| | | | | |"},
{"| |_________ _____ |"},
{"| _______ __|"},
{"|_______________ |"},
{"|_____ _______________| __|"},
{"| | _______ |"},
{"| | ____________ ________|"},
{"| | # |"},
{"| | | __|________ |"},
{"|______|______________________|"}
};
};
Player.h
#pragma once
#include "Grid.h"
#include "Enemy.h"
extern char y;
extern char x;
extern char m_grid[18][32];
class Player
{
public:
Player();
~Player();
static const char avatar = '@';
void Set_difficulty();
void Move(int speed);
int m_speed{ 0 };
struct Vector2
{
int y{ 0 };
int x{ 1 };
int new_y{ 0 };
int new_x{ 0 };
};
Vector2* playerPos = nullptr;
int m_score{ 0 };
int m_highscore{ 0 };
void Print_score();
private:
char m_difficulty = ' ';
};
Player.cpp
#include <iostream>
#include "Player.h"
Player::Player()
{
m_grid[playerPos.y][playerPos.x] = avatar;
}
Player::~Player()
{
}
void Player::Set_difficulty()
{
std::cout << "Pick your difficulty:
H -> Hard
N -> Normal
E -> Easy
Input : ";
std::cin >> m_difficulty;
if (m_difficulty == 'e' || m_difficulty == 'E')
{
m_speed = 3;
}
else if (m_difficulty == 'n' || m_difficulty == 'N')
{
m_speed = 2;
}
else
{
m_speed = 1;
}
}
void Player::Move(int speed)
{
if (GetAsyncKeyState(VK_DOWN))
{
if (m_grid[playerPos->y+1][playerPos->x] == ' ' || m_grid[playerPos->y + 1][playerPos->x] == '*') //if desired space is empty or has a coin
{
playerPos->new_y = playerPos->y + speed; //new player pos = current pos + speed
m_grid[playerPos->y][playerPos->x] == ' '; //old space is now empty
}
}
if (GetAsyncKeyState(VK_UP))
{
if (m_grid[playerPos->y-1][playerPos->x] == ' ' || m_grid[playerPos->y-1][playerPos->x] == '*')
{
playerPos->new_y = playerPos->y - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_RIGHT))
{
if (m_grid[playerPos->y][playerPos->x+1]== ' ' || m_grid[playerPos->y][playerPos->x+1] == '*')
{
playerPos->new_x = playerPos->x + speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
if (GetAsyncKeyState(VK_LEFT))
{
if (m_grid[playerPos->y][playerPos->x-1] == ' ' || m_grid[playerPos->y][playerPos->x-1] == '*')
{
playerPos->new_x = playerPos->x - speed;
m_grid[playerPos->y][playerPos->x] == ' ';
}
}
}
void Player::Print_score()
{
}
答案
首先:grid.h和player.h相互包含。应该避免这种情况,在这种情况下,grid.h不需要player.h。
实际问题:播放器没有可以使用的Grid对象。理想情况下,Grid和Player应该是Game类的成员,并且对Grid对象的引用应该传递给Player。因此可以使用Grid :: m_grid。现在,您使用另一个仅声明为外部的m_grid,因此没有实际的实例化。如果没有Game类,可以在main()中完成一个简单的练习,但是您确实需要声明一个Grid对象来访问它,并且如果玩家想基于网格移动,则需要玩家对其进行引用。
以上是关于c ++:OOP:如何在单独的头文件中更新数组?的主要内容,如果未能解决你的问题,请参考以下文章
Visual Studio C++ - 运行单独的 .cpp 文件?