-4

播放器.h

#ifndef HEADER_GUARD_PLAYER
#define HEADER_GUARD_PLAYER

#include <string>
using std::string;


//Parent player class
class Player
{
      private:        
      protected:
         string playerName; 
         int chipCount;

      public:
         //Constructor
         Player();
         Player(string name);

}; 

//Custom Players

class PlayerSteven: public Player
{
};
class PlayerBlake: public Player
{
};
class PlayerTucker: public Player
{
};
#endif

播放器.cpp

#include "Player.h"
#define STARTCHIPS 1500

Player::Player()
{
     chipCount = STARTCHIPS;
     playerName = "Default";          
};
Player::Player(string name)
{
     playerName = name;
};

主文件

#include <iostream>
#include "Deck.h"
#include "Player.h"
#include <string>

using namespace std;

int main()
{
    Deck deck;
    PlayerSteven steven = new PlayerSteven("Steven");
    PlayerBlake blake = new PlayerBlake("Blake");
    PlayerTucker tucker = new PlayerTucker("Tucker");

    deck.shuffle();
    int x = 0;
    int y = 0;
    for (int i = 0; i < 52; i++)
    {
        deck.nextCard(x,y);
        cout << x << "," << y << endl;
    }
    system("PAUSE");
    return 0;
}

错误:

11 C:\Users\Documents\temp\p_comp.cpp 没有匹配函数调用“PlayerSteven::PlayerSteven(const char[7])”

11 C:\Users\Documents\temp\p_comp.cpp 没有匹配函数调用“PlayerSteven::PlayerSteven(const char[7])”

注意 C:\Dev-Cpp\PROJECTS\Player.h:26 PlayerSteven::PlayerSteven(const PlayerSteven&)

有人知道我怎样才能编译它吗?

谢谢。

4

2 回答 2

2

在 C++ 中,构造函数不是继承的。所以你必须将它们添加到子类中:

class PlayerSteven: public Player
{
public:
    PlayerSteven()
    {}
    PlayerSteven(string name)
    :Player(name)
    {}
};

对于 的其他子类也是如此Player

如果你有足够新的 C++11 编译器,你实际上可以继承构造函数,但你必须明确地这样做:

class PlayerSteven : public Player
{
public:
    using Player::Player;
};
于 2013-04-30T14:40:36.787 回答
1

您需要为派生类型声明和定义构造函数:

class PlayerSteven: public Player
{
  PlayerSteven() {}
  PlayerSteven(const std::string& s) : Player(s) {}
};

等等。在 C++11 中,您可以使用继承的构造函数

class PlayerSteven: public Player
{
  using Player::Player;
};

接下来,在您的主要内容中,您尝试PlayerSteven从指向的指针初始化对象PlayerSteven

PlayerSteven steven = new PlayerSteven("Steven");

您没有构造函数来执行这样的转换,无论如何您可能都不想要。您可以只实例化一个PlaverSteven对象:

PlayerSteven steven("Steven");
于 2013-04-30T14:41:19.017 回答