本文介绍了C ++中类定义的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a bit of a problem here. I'm trying to define several classes, of which some are Players and some are Pawns belonging to the players. Coming from Python, I'm used to being able to conveniently access a Pawn's owning Player through the Pawn, as well as accessing a Player's Pawns through the Player. Correct me if I'm wrong, but this seems impossible in C++.

我当前首先定义了Player,它的数据成员之一m_Pawns应该是vector<Pawn>.我声明了数据成员,但没有给它赋任何值.我还定义了一个成员函数,该函数旨在将典当向量分配给m_Pawns,但是在构造函数附近的任何地方都不会调用它.因为我实际上没有在Player的构造函数中调用Pawn的构造函数,所以看来我应该没事.

I currently define Player first, and one of its data members m_Pawns is supposed to be a vector<Pawn>. I declare the data member, but I don't assign it any value. I also define a member function that is meant to assign a vector of pawns to m_Pawns, but I don't call it anywhere near the constructor. Since I'm not actually calling the constructor for Pawn in the constructor for Player, it seems I should be fine.

这是我的Player课. Board类是事先定义的,而Pawn类是事后定义的(Pawn类包含指向Player类的所有者的指针,因此切换它实际上并没有帮助).

Here's my Player class. The Board class is defined beforehand, whereas the Pawn class is defined afterwards (the Pawn class contains pointers to an owner of the Player class, so switching it around doesn't really help).

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 i = 0; i < 4; ++i)
        {
            m_Pawns.push_back(Pawn((*board).Cluster1[i], this*, m_Color));
        }
        break;
    case 2:
        for(int i = 0; i < 4; ++i)
        {
            m_Pawns.push_back(Pawn((*board).Cluster2[i], this*, m_Color));
        }
        break;
    case 3:
        for(int i = 0; i < 4; ++i)
        {
            m_Pawns.push_back(Pawn((*board).Cluster3[i], this*, m_Color));
        }
        break;
    default:
        cout << "Invalid player ID!\n\n";
        break;
    }
}

推荐答案

如果首先出现class Player,随后出现class Pawn,则可以仅声明指针或对更高级别类的引用 (此处为Pawn).您不能有上等课程的对象,例如

If the class Player is coming first and class Pawn coming later then you can only declare pointer or reference to the later class (here Pawn). You cannot have objects of later class, e.g.

class Player {
  Pawn* p; // allowed
  Pawn& r; // allowed
  vector<Pawn*> p; // allowed
  vector<Pawn&> vr; // not allowed (reference are not copyable)
  vector<Pawn> o; // error !
};

class Pawn {};

没有办法可以克服这种情况,因为在non-template类的C ++中,需要显示完整的定义来声明对象.

There is no way you can overcome this situation, as in C++ for non-template class one need to show full definition to declare objects.

唯一的出路是重新格式化代码或使用指针/引用(带有前向声明).

The only way out is to reformat your code or use pointer/reference (with forward declaration).

这篇关于C ++中类定义的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:52