错误:没有匹配的函数可用于调用“CustomerData::CustomerData()”

发布于 2024-12-18 04:39:59 字数 1135 浏览 4 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余生一个溪 2024-12-25 04:39:59

使用标准地图模板的 operator[] 默认情况下,如果不存在给定键,则构造一个具有给定键的元素。

您的元素类型没有默认构造函数,因此不会发生这种情况。

您必须改用.insert

customersDb.insert(make_pair(data.number(), data));

我无法弄清楚并且无法使用插入,因为使用 [] 括号访问元素也不起作用。

要修改现有元素,请使用 .find 检索迭代器,取消引用并执行您的操作。

yourMapType::iterator it = customersDb.find(data.number());
if (it != customersDb.end())
   *it = data;

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.

customersDb.insert(make_pair(data.number(), data));

I can't figure it out and can't use insert because accessing element with [] brackets doesn't work either.

To modify an existing element, retrieve an iterator with .find, dereference and do your thing.

yourMapType::iterator it = customersDb.find(data.number());
if (it != customersDb.end())
   *it = data;
断爱 2024-12-25 04:39:59

它不起作用,因为 operator[] 实际上首先默认构造该值,然后分配给它。


您可以提供默认构造函数,或使用此调用:

customersDb.insert( std::map< uint, CustomerData >::value_type (data.number(), data) );

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:

customersDb.insert( std::map< uint, CustomerData >::value_type (data.number(), data) );
我偏爱纯白色 2024-12-25 04:39:59

您需要为 CustomerData 类提供默认构造函数。

CustomerData::CustomerData()
{
}

或者,您以仅调用您为类提供的构造函数的方式实现代码。 [] 需要它来默认构造一个元素。

第一个看起来是更好的选择。

错误原因:

当您为自己的类提供任何构造函数时,编译器不会生成默认的无参数构造函数,而您的代码需要一个构造函数,因此会出现错误。

You need to provide a default constructor for your CustomerData class.

CustomerData::CustomerData()
{
}

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.

朱染 2024-12-25 04:39:59

阅读错误,它就在那里:)

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&)

但是两者都带有参数,而您没有传递这些参数。

Read the error, it's in there :)

stl_map.h:450: error: no matching function for call to 'CustomerData::CustomerData()'

It looks like the compiler thinks you're not actually passing any information to the function.

It does recognize these two alternatives :

CustomerData.h:18: candidates are: CustomerData::CustomerData(CustomerData&)
CustomerData.h:17: note: CustomerData::CustomerData(uint, const std::string&)

But both take arguments, which you're not passing.

简单 2024-12-25 04:39:59

您需要确保有默认(空白)构造函数,以便可以在没有参数的情况下构造类型。

myclass(); //implement somewhere

您还可以在 C++11 中提供默认参数,

myclass(const std::string& value="hello"); //implement somewhere

您可以这样做

myclass()=default; //implementation will be generated

You need to make sure there is default (blank) constructor so the type can be constructed without args.

myclass(); //implement somewhere

you can also provide default args

myclass(const std::string& value="hello"); //implement somewhere

in C++11 you can do this

myclass()=default; //implementation will be generated
給妳壹絲溫柔 2024-12-25 04:39:59

它试图调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文