重载构造函数的调用不明确,为什么?

时间:2020-11-01 15:25:34

标签: c++

我一直在用C ++练习类,我创建了两个构造函数:一个是无参数构造函数,而另一个则需要一些参数,当我创建类名的变量并且不放置任何参数时,我希望它能够使用no args构造函数,但不这样做,它表示重载的 Fight()的调用不明确

这是课程的头文件

#ifndef _FIGHT_H
#define _FIGHT_H
#include <iostream>
#include <string>
class Fight{
private:
    friend void Player_statt(Fight obj);
    std::string name;
    std::string gender;
    int xp;
    int level;
    int magicka;
    long int damage;
public:
    Fight(std::string _name = "Bot", std::string gender = "Male", int _xp = 0, int _level = 0, int _magicka = 0, long int _damage = 0);
    Fight();
    Fight(const Fight &source);
    std::string get_name(){
        return name;
    }
};
       
#endif //_FIGHT_H

这是定义

#include "Fight.h"
#include <iostream>

//No Args Constructor
Fight::Fight(){
    name = "Bot";
    gender = "Male";
    xp = 0;
    level = 0;
    magicka = 0;
    damage = 0;
}

//Args Constructor
Fight::Fight(std::string _name, std::string _gender, int _xp, int _level, int _magicka, long int _damage){
    name = _name;
    gender = _gender;
    xp = _xp;
    level = _level;
    magicka = _magicka;
    damage = _damage;
}

//Copy Constructor
Fight::Fight(const Fight &source)
     :name{source.name}, gender{source.gender}, xp{source.xp}, level{source.level}, magicka{source.magicka}, damage{source.damage}
     {
    
}

//STAT FUNCTION
//void Fight::Player_statt(Fight obj){
//    std::cout << "Name: " << obj.name << std::endl;
//    std::cout << "Xp: " << obj.xp << std::endl;
//    std::cout << "Level: " << obj.level << std::endl;
//    std::cout << "Magicka: " << obj.magicka << std::endl;
//    std::cout << "Damage: " << obj.damage << std::endl;
//}

这里是我的主要生活

#include <iostream>
#include "Fight.h"

using namespace std;

void Player_statt(Fight obj){
    std::cout << "Name: " << obj.name << std::endl;
    cout << "Gender: " << obj.gender << endl;
    std::cout << "Xp: " << obj.xp << std::endl;
    std::cout << "Level: " << obj.level << std::endl;
    std::cout << "Magicka: " << obj.magicka << std::endl;
    std::cout << "Damage: " << obj.damage << std::endl;
}
int main(){
    Fight barn;
    return 0;
}

0 个答案:

没有答案