gpt4 book ai didi

c++ - 传递一个空的 vector 参数

转载 作者:行者123 更新时间:2023-11-30 02:04:40 31 4
gpt4 key购买 nike

我一直在使用这个类:

class DogTrainer 
{
public:
DogTrainer(int identity,const std::string& nom, const std::vector<Dog*> dogz) :
idD(identity),
name(nom),
trainees(dogz)
{ };

~DogTrainer();

private:
int idD;
string name;
std::vector<Dog*> trainees;
};

但有时当我想实例化一个新对象时,我不需要传递一个“trainees”参数,所以我想有这样的可能性

DogTrainer* Trainer=new DogTrainer(my_id, my_name);

因此我尝试更改我的 DogTrainer 构造函数

DogTrainer(int identity,const std::string& nom, const std::vector<Dog*> dogz="") :
idD(identity),
name(nom),
trainees(dogz)
{ };

但是它没有用,所以请帮忙!

最佳答案

将您的构造函数声明为:

DogTrainer(int identity,const std::string& nom,
const std::vector<Dog*> dogz = std::vector<Dog*>());

""const char* , 和一个 std::vector不能由此构造。

顺便说一句,dogz 没有太多意义作为const std::vector<Dog*> .要么让它成为非 const或将其设为 const引用。

关于c++ - 传递一个空的 vector 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10347915/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com