C++:通过引用传递对象作为类参数

问题描述 投票:0回答:1

我是 C++ 新手。我正在尝试在终端中实现一个简单的游戏,其中

Board
*
分隔,并且
Pawn
可以使用 zqsd 键在棋盘中移动。

我希望我的

Board
有一个
Pawn
参数,这样当我
displayBoard()
时,棋盘就知道棋子在哪里,这要归功于
Pawn.getXpos()
Pawn.getYpos()

要在

main.cpp
中执行此操作,我首先创建一个位置为 (1, 1) 的
Pawn
对象,然后创建一个 20 个字符方形的板对象。之后,我使用
baord.addNewPawn(pawn)
将 pawn“分配”到棋盘,这就是问题所在,因为我看到 main.cpp 中的初始 pawn 和棋盘参数的棋子不是同一个对象(因为地址不同)。我知道这与地址和指针有关,但我尝试的任何方法都不起作用。

有什么帮助吗?

main.cpp

#include <iostream>

#include "Board.h"
#include "Pawn.h"
#include <conio.h>

using namespace std;

int main()
{
    Pawn pawn{ 1, 1 };
    Board board{ 20, 20 };

    board.addNewPawn(pawn);
    board.initBoard();
    board.displayBoard();

    char input;
    do
    {
        input = _getch();

        int xshift = 0;
        int yshift = 0;

        if (input == 'z')
        {
            yshift = -1;
        }
        else if (input == 'q')
        {
            xshift = -1;
        }
        else if (input == 's')
        {
            yshift = 1;
        }
        else if(input == 'd')
        {
            xshift = 1;
        }
        
        cout << "pawn address " << &pawn << endl;
        pawn.movePawn(xshift, yshift);
        board.displayBoard();

    }while (input != 'x');
    


}

Board.cpp
+
Board.h

#include <iostream>
#include <vector>

#include "Board.h"

using namespace std;

Board::Board(int x, int y): m_xsize(x), m_ysize(y)
{
}

void Board::initBoard()
{
    for (int x=0; x<m_xsize; x++)
    {
        vector<char> newline(m_ysize);
        m_board.push_back(newline);
    }

    for (int x = 0; x < m_xsize; x++)
    {
        for (int y = 0; y < m_ysize; y++)
        {
            if (x == 0 || y == 0 || x == m_xsize - 1 || y == m_ysize - 1)
            {
                m_board[x][y] = '*';
            }
            else
            {
                m_board[x][y] = ' ';
            }
        }
    }

}

void Board::displayBoard()
{
    cout << "board pawn address " << &m_pawn << endl;

    m_board[m_pawn.getXpos()][m_pawn.getYpos()] = 'o';
    cout << "display " << m_pawn.getXpos() << " " << m_pawn.getYpos() << endl;

    for (int x = 0; x < m_xsize; x++)
    {
        for (int y = 0; y < m_ysize; y++)
        {
            cout << m_board[x][y];
        }
        cout << endl;
    }
}

void Board::addNewPawn(Pawn pawn)
{
    m_pawn = pawn;
}

#ifndef BOARD
#define BOARD
#include<vector>
#include "Pawn.h"

class Board
{
public:
    Board(int x, int y);

    void initBoard();
    void displayBoard();
    void addNewPawn(Pawn pawn);


private:
    std::vector<std::vector<char>> m_board;
    int m_xsize;
    int m_ysize;
    Pawn m_pawn;
};
#endif

Pawn.cpp
+
Pawn.h

#include <iostream>
#include <vector>

#include "Pawn.h"

using namespace std;

Pawn::Pawn()
{
}
    
Pawn::Pawn(int x, int y): m_xpos(x), m_ypos(y)
{
}

void Pawn::receiveDamage()
{
    m_hp -= 1;
}

void Pawn::movePawn(int x, int y)
{
    
    m_xpos += x;
    m_ypos += y;

    cout << "move pawn " << m_xpos << " " << m_ypos << endl;
}

void Pawn::reward(int turns)
{
    // here
}


int Pawn::getXpos()
{
    return m_xpos;
}

int Pawn::getYpos()
{
    return m_ypos;
}
#ifndef PAWN
#define PAWN

#include <vector>

class Pawn
{
public:
    Pawn();
    Pawn(int x, int y);

    void receiveDamage();
    void movePawn(int x, int y);
    void reward(int turns);
    int getXpos();
    int getYpos();

private:
    int m_hp = 5;
    int m_xpos = 1;
    int m_ypos = 1;
    std::vector<int> m_history_x;
    std::vector<int> m_history_y;
};

#endif
c++ oop pointers
1个回答
0
投票

感谢@Caleth @HolyBlackCat 和@Wick 我让它工作了:

Board.h
中,我使用了指针
Pawn *m_pawn_ptr
,并给出了
addNewPawn
我的对象的地址。

#ifndef BOARD
#define BOARD
#include<vector>
#include "Pawn.h"

class Board
{
public:
    Board(int x, int y);

    void initBoard();
    void displayBoard();
    void addNewPawn(Pawn &pawn);


private:
    std::vector<std::vector<char>> m_board;
    int m_xsize;
    int m_ysize;
    Pawn *m_pawn_ptr;
};
#endif

然后为了访问 Board.cpp 中的 Pawn,我使用了箭头运算符,例如

m_pawn_ptr->getXpos()
。有效 ! 非常感谢你

© www.soinside.com 2019 - 2024. All rights reserved.