小编Gar*_*ckW的帖子

C++中的类定义顺序

我在这里遇到了一些问题.我正在尝试定义几个类,其中一些是玩家,一些是属于玩家的Pawns.来自Python,我习惯于通过Pawn方便地访问Pawn拥有的玩家,以及通过玩家访问玩家的Pawns.如果我错了,请纠正我,但在C++中这似乎是不可能的.

我目前首先定义Player,其中一个数据成员m_Pawns应该是a vector<Pawn>.我声明了数据成员,但我没有给它任何值.我还定义了一个成员函数,用于分配一个pawn的向量m_Pawns,但我不会在构造函数附近的任何地方调用它.因为我实际上并没有在构造函数Pawn中调用构造函数Player,所以看起来我应该没问题.

这是我的Player课.在Board类预先定义的,而Pawn类是事后定义(在Pawn类包含指针指向的所有者Player类,所以它的开关周围并没有真正帮助).

class Player
{
public:
    Player(sf::Color color, const string& name);
    sf::Color GetColor();
    string GetName();
    void CreatePawns(Board& board, int which);
protected:
    vector<Pawn> m_Pawns;
    sf::Color m_Color;
    string m_Name;
};

Player::Player(sf::Color color, const string& name):
    m_Color(color),
    m_Name(name)
{}

sf::Color Player::GetColor()
{
    return m_Color;
}

string Player::GetName()
{
    return m_Name;
}

void Player::CreatePawns(Board& board, int which)
{
    switch(which)
    {
    case 1:
        for(int …
Run Code Online (Sandbox Code Playgroud)

c++ pointers reference class definition

5
推荐指数
1
解决办法
2148
查看次数

会员未在范围内宣布?

所以在完成一本介绍性的书之后,我正试着用一些C++,而且我已经陷入了困境.我已经创建了一个对象矢量,每个对象都有一个SFML圆对象作为成员,我希望main()去绘制这些圆.调用向量theBoard,但是当我尝试访问它时,我收到以下错误消息:

error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*'
error: 'theBoard' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

我是新手(来自两年的Python),所以我确定我在某个地方犯了一个错误.以下是电路板创建的相关代码:

class Board
{
public:
    //These are the member functions.
    Board();
    ~Board();
    vector<Space*> CreateBoard();
    //This will be the game board.
    vector<Space*> theBoard;
    //These clusters represent the waiting areas for pieces not yet in the game.
    vector<Space*> Cluster1;
    vector<Space*> Cluster2;
    vector<Space*> Cluster3;
private:
    //These integers represent the number of spaces on each row, starting at the top …
Run Code Online (Sandbox Code Playgroud)

c++ pointers declaration vector sfml

3
推荐指数
1
解决办法
2301
查看次数

我弄乱了我的遗产

所以我有一个Piece类应该代表棋盘上的棋子,我计划从中继承另外两个类.不过,我在这方面遇到了很多问题; 这是迄今为止的相关代码.

///
/// PIECE CLASS HERE
/// this is an abstract class from which Barrier and Pawn inherit.

class Piece
{
public:
    Piece(Space* start);
    sf::Shape m_Circle;
protected:
    int m_X;
    int m_Y;
    int m_radius;
    Space* CurrentSpace;
};

Piece::Piece(Space* start):
    m_X(start->GetX()),
    m_Y(start->GetY()),
    m_radius(14),
    CurrentSpace(start)
{}

///
/// BARRIER CLASS HERE
/// these are the white stones that block a player's path

class Barrier : public Piece
{
public:
    Barrier(Space* initial);
    void Move(Space* target, bool isCapturing);
};

Barrier::Barrier(Space* initial)
{
    Piece(initial);
    m_Circle …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor sfml

2
推荐指数
1
解决办法
227
查看次数

对于循环只是......没有开始?

这是一个奇怪的问题.我有一个执行for循环的void函数,没有别的东西,但for循环不会启动,即使函数被调用.这是功能:

void Cell::Consolidate()
{
    cout << "Consolidating (outside)...\n";
    for(int i = 0; i < m_Tiles.size(); ++i)
    {
        cout << "Consolidating (inside)...\n";
        int row = m_Tiles[i]->GetRow();
        int col = m_Tiles[i]->GetCol();
        //Check below.
        if((*m_pTileMap)[row + 1][col].pParentCell != this)
        {
            m_EdgeTiles.push_back(m_Tiles[i]);
            m_Tiles[i]->SetColor(sf::Color(100, 100, 100));
            bool newNeighbor = true;
            for(int j = 0; j < m_Neighbors.size(); ++j)
            {
                if(m_Neighbors[j] == (*m_pTileMap)[row + 1][col].pParentCell)
                {
                    newNeighbor = false;
                    break;
                }
            }
            if(newNeighbor)
            {
                m_Neighbors.push_back((*m_pTileMap)[row + 1][col].pParentCell);
            }
        }
        //Check above.
        else if((*m_pTileMap)[row - 1][col].pParentCell != …
Run Code Online (Sandbox Code Playgroud)

c++ loops for-loop

1
推荐指数
1
解决办法
138
查看次数