No matching call to default constructor, when using QVector

不想你离开。 提交于 2019-11-28 12:54:54

As with many Qt containers, QVector's element type must be an assignable data type in your version.

Unlike the standard library, Qt defines this as:

The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator.

This is really unfortunate, because there's no practical need for a default constructor in your example, and indeed a std::vector would (compliantly) let you use an element type that doesn't have one.

The QVector::value(int) function does rely on this property, but you're not using it! The Qt devs must be doing some kind of checks up-front, rather than taking the standard library's approach of "just check preconditions when they're actually needed", or else this is an "accident" of the code!

As a consequence, until 5.13 in which this was changed, you will have to give A a default constructor, sorry.

Don't forget a copy constructor, too… and a proper qualification on that A::function() definition.

A forward declaration will not solve this, neither do you need one. In fact, adding one to this particular program will do literally nothing. ;)

First in a.cpp update function definition:

void A::function(){  // A: added
    //Do something.
}

Second, I would add A(const A&) copy constructor since list may need this for internal buffer reallocation purposes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!