错误:没有匹配的函数可用于调用“CustomerData::CustomerData()”
这是我的 CreateCustomer 方法:
CustomerData Database::createCustomer(std::string const& name) {
CustomerData data(customersDb.size()+1, name);
customersDb[data.number()] = data;
// customersDb.insert(std::pair<uint, CustomerData>(data.number(), data));
return data;
}
CustomerData 类的构造函数:
CustomerData::CustomerData(uint number, std::string const& name) {
m_number = number;
m_name = name;
}
这一行不正确:
customersDb[data.number()] = data;
事实是,下一行有效。我无法弄清楚并且无法使用插入,因为使用 [] 括号访问元素也不起作用。
我什至尝试过创建复制构造函数,但没有效果:
CustomerData::CustomerData(CustomerData &data) {
m_number = data.number();
m_name = data.name();
}
这些是错误:
Database.cpp:5: instantiated from here
stl_map.h:450: error: no matching function for call to 'CustomerData::CustomerData()'
CustomerData.h:18: candidates are: CustomerData::CustomerData(CustomerData&)
CustomerData.h:17: note: CustomerData::CustomerData(uint, const std::string&)
我使用 Mingw。
This is my CreateCustomer method:
CustomerData Database::createCustomer(std::string const& name) {
CustomerData data(customersDb.size()+1, name);
customersDb[data.number()] = data;
// customersDb.insert(std::pair<uint, CustomerData>(data.number(), data));
return data;
}
Constructor of class CustomerData:
CustomerData::CustomerData(uint number, std::string const& name) {
m_number = number;
m_name = name;
}
Incorrect line is this one:
customersDb[data.number()] = data;
Thing is, the next line works. I can't figure it out and can't use insert because accessing element with [] brackets doesn't work either.
I've tried even creating copy constructor but with no effect:
CustomerData::CustomerData(CustomerData &data) {
m_number = data.number();
m_name = data.name();
}
These are the errors:
Database.cpp:5: instantiated from here
stl_map.h:450: error: no matching function for call to 'CustomerData::CustomerData()'
CustomerData.h:18: candidates are: CustomerData::CustomerData(CustomerData&)
CustomerData.h:17: note: CustomerData::CustomerData(uint, const std::string&)
I use Mingw.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用标准地图模板的
operator[]
默认情况下,如果不存在给定键,则构造一个具有给定键的元素。您的元素类型没有默认构造函数,因此不会发生这种情况。
您必须改用
.insert
。要修改现有元素,请使用
.find
检索迭代器,取消引用并执行您的操作。operator[]
with the standard map template default-constructs an element with the given key if none already exists.Your element type has no default constructor, so this cannot happen.
You'll have to use
.insert
instead.To modify an existing element, retrieve an iterator with
.find
, dereference and do your thing.它不起作用,因为
operator[]
实际上首先默认构造该值,然后分配给它。您可以提供默认构造函数,或使用此调用:
It doesn't work because
operator[]
actually first default constructs the value, and then assigns to it.You can either provide a default constructor, or use this call:
您需要为
CustomerData
类提供默认构造函数。或者,您以仅调用您为类提供的构造函数的方式实现代码。
[]
需要它来默认构造一个元素。第一个看起来是更好的选择。
错误原因:
当您为自己的类提供任何构造函数时,编译器不会生成默认的无参数构造函数,而您的代码需要一个构造函数,因此会出现错误。
You need to provide a default constructor for your
CustomerData
class.Or you implement a code in such a way that it only calls the constructor which you have provided for your class. It is needed by
[]
to default construct an element.First one looks a better option.
Reason for error:
When you provide any constructor for your own class, the compiler does not generate the default no argument constructor and your code needs one and hence the error.
阅读错误,它就在那里:)
看起来编译器认为您实际上没有向函数传递任何信息。
它确实识别了这两种选择:
但是两者都带有参数,而您没有传递这些参数。
Read the error, it's in there :)
It looks like the compiler thinks you're not actually passing any information to the function.
It does recognize these two alternatives :
But both take arguments, which you're not passing.
您需要确保有默认(空白)构造函数,以便可以在没有参数的情况下构造类型。
您还可以在 C++11 中提供默认参数,
您可以这样做
You need to make sure there is default (blank) constructor so the type can be constructed without args.
you can also provide default args
in C++11 you can do this
它试图调用 CustomerData 类的构造函数,但它不存在。我很确定这与标准地图的内部工作方式有关。映射的值必须有一个默认构造函数。这通常是由编译器生成的,但由于您已经提供了另一个构造函数,所以它不会生成。
It is trying to call the constructor on your CustomerData class, but it doesn't exist. I'm pretty sure this is something to do with how the standard map works internally. The value of the map must have a default constructor. This would normally be generated by the compiler, but as you have already provided another constructor it was not generated.