c++ - 在构造函数中 'Player::Player(std::string, int, Board*)' :

标签 c++ constructor

Player::Player(string Playername, int nr ,Board* x)
{
    imie= Playername;
    number=nr;
    int k;

    if(nr==1)
        k=0;
    else k=24;

    for(int i=0;i<UNITS_NUMBER;i++)
    {   Unit figure(figure,i);
        units[i]=figure;

        units[i].move(x->getF(k));
        if(nr==1)
            k++;
        else k--;
    }
}

(只是Player类的构造函数) 和错误:

在构造函数Player::Player(std::string, int, Board*)中:

没有匹配函数来调用 Unit::Unit()

Player::Player(string Playername, int nr ,Board* x)

这里是Unit的定义

class Unit
{  
    Field* place;
    Unit* unit;
    int name;


public:
    Unit(Unit iam, int n);
    void move(Field* x);
    int getname(){return name;}
};

这里是Player的定义

const int UNITS_NUMBER = 5;
class Player
{
    Unit units[UNITS_NUMBER];
    string imie;
    int number;

public:
    Player(string Playername, int nr, Board *x);
    string getName();
    void decide(Board*);
    ~Player();
};

如果你需要更多,告诉我。

更改后:

class Unit
{  Field* place;
    Unit* unit;
    int name;


public:
    Unit(int n);
    void move(Field* x);
    int getname();
};

播放器

Player::Player(string Playername, int nr ,Board* x)
{
    imie= Playername;
    number=nr;
    int k;
    if(nr==1)
        k=0;
    else k=24;

    for(int i=0;i<UNITS_NUMBER;i++)
    {   Unit figure(i);
        units[i]=figure;

        units[i].move(x->getF(k));
        if(nr==1)
            k++;
        else k--;
    }
}

还是一样的错误;/

最佳答案

Unit 没有复制构造函数(因此您不能按值传递它)。

此外,这是未定义的行为:

Unit figure(figure,i);

因为 figure 的值在初始化之前就被使用了。

关于c++ - 在构造函数中 'Player::Player(std::string, int, Board*)' :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19529115/

相关文章:

c++ - Set表现异常:本地还是非本地?

c++ - 强制函数只接受一组特定的数据类型

c++ - 如何处理非平凡析构函数类型的可变参数模板构造函数

javascript - 在 1 行代码中自定义构造函数

c++ - 六边形网格上 2 个六边形之间的距离

c++ - Qt错误: LNK1181: cannot open input file 'debug\main.obj'

java - 更改 Eclipse 中的构造函数模板,以便它调用 setter

c++ - 如何在构造函数中初始化 C++ 对象成员变量?

c++ - 部分特化显式模板特化

PHP > 5.4 : overriding constructor with different signature