2

I am having trouble compiling the following:

#include <QVector>
#include <QDebug>
#include <vector>

class item
{
    int var;
public:
    //Without default constructor this program will not compile
    //item(){}

    item(int value)
    {
        var = value;
    }
    int getVar()
    {
        return var;
    }
};

int main()
{
    //This code will not compile
    QVector<item> y;
    y.append(item(1));
    qDebug() << y[0].getVar();

    //std::vector however will work despite an absence of a default contructor
    std::vector<item> z;
    z.push_back(item(1));
    qDebug() << z.at(0).getVar();

    return 0;
}

To be precise the append line will not compile.

Why item has to have a default contructor in this case?

D. Konov
  • 53
  • 4
  • Probably to allow `QVector::resize` to be able to default construct `item`s. – user4581301 Jan 18 '19 at 00:01
  • @user4581301 But the same thing should be applicable for `std::vector` also. Right? – Kunal Puri Jan 18 '19 at 00:04
  • @KunalPuri good point. I need to turn off C++11 and check something. – user4581301 Jan 18 '19 at 00:06
  • 1
    `vector` only complains if you use `resize` or similar that force default construction. `QVector` may have more going on behind the scenes. I'm going to have to leave this one to folks with a much higher level of familiarity with QT. – user4581301 Jan 18 '19 at 00:10
  • [What is the reason of QVector's requirement for default constructor?](https://stackoverflow.com/questions/33380402/what-is-the-reason-of-qvectors-requirement-for-default-constructor) may be of use. – user4581301 Jan 18 '19 at 00:11
  • [No matching call to default constructor, when using QVector](https://stackoverflow.com/questions/54181249/no-matching-call-to-default-constructor-when-using-qvector) seems to suggest that this is no longer a hard requirement in future versions of QT. – user4581301 Jan 18 '19 at 00:14
  • @KunalPuri downvotes / upvotes can and should be anonymous so the request for justification is currently considered: *it's no longer needed* since it does not contribute anything, for more information check [Why isn't providing feedback mandatory on downvotes, and why are ideas suggesting such shot down?](https://meta.stackoverflow.com/questions/357436/why-isnt-providing-feedback-mandatory-on-downvotes-and-why-are-ideas-suggestin) – eyllanesc Jan 18 '19 at 00:37
  • @eyllanesc I agree with your point. But I have a doubt. Whenever anyone downvotes an answer, stackoverflow recommends to leave a comment. Then why that doesn't apply for questions? – Kunal Puri Jan 18 '19 at 00:56
  • @KunalPuri Good question, maybe in [meta](https://meta.stackoverflow.com/) have an answer, some time ago I do not give downvotes to answers. but I think it's a suggestion, not an obligation. – eyllanesc Jan 18 '19 at 01:06
  • Possible duplicate of [No matching call to default constructor, when using QVector](https://stackoverflow.com/questions/54181249/no-matching-call-to-default-constructor-when-using-qvector) – ymoreau Jan 29 '19 at 12:32

1 Answers1

4

The reason why std::vector works differently lies in the fact that in vector, raw uninitialized memory is allocated and then calls copy constructor to do the copy whenever required. This process doesn't require calling default constructor for resize(). That's why there is no dependency as such on default constructor.

For more info, See AnT's answer here.

QVector requires type to be default constructible because of the way the internal function realloc() is implemented.

Source: Understanding the Qt containers

Kunal Puri
  • 3,419
  • 1
  • 10
  • 22