错误:隐式声明的复制构造函数的定义

Nic*_*rin 5 c++ qt copy-constructor

我正在解决目前正在研究的Qt C++项目问题.这是我要介绍的一个新部分,我发现它有点令人困惑.我创建了一些由Stock,Bond和Savings类继承的类Asset.这一切都没问题.然后我创建了一个名为AssetList的类,它派生了QList,这个类是我发现问题的地方.

这是我到目前为止的代码.

AssetList.h

#ifndef ASSET_LIST_H
#define ASSET_LIST_H

#include "Asset.h"
#include <QString>

class AssetList : public QList<Asset*>
{
public:
    AssetList(){}
    ~AssetList();
    bool addAsset(Asset*);
    Asset* findAsset(QString);
    double totalValue(QString);
};

#endif
Run Code Online (Sandbox Code Playgroud)

AssetList.cpp

#include "AssetList.h"

AssetList::AssetList(const AssetList&) : QList<Asset*>(){}
AssetList::~AssetList()
{
    qDeleteAll(*this);
    clear();
}

bool AssetList::addAsset(Asset* a)
{
    QString desc = a->getDescription();
    Asset* duplicate = findAsset(desc);

    if(duplicate == 0)
    {
        append(a);
        return true;
    }
    else
    {
        delete duplicate;
        return false;
    }
}

Asset* AssetList::findAsset(QString desc)
{
    for(int i = 0 ; i < size() ; i++)
    {
        if(at(i)->getDescription() == desc)
        {
            return at(i);
        }
    }

    return 0;
}

double AssetList::totalValue(QString type)
{
    double sum = 0;

    for(int i = 0 ; i < size() ; i++)
    {
        if(at(i)->getType() == type)
        {
            sum += at(i)->value();
        }
    }

    return sum;
}
Run Code Online (Sandbox Code Playgroud)

我目前得到的错误是编译错误:error: definition of implicitly declared copy constructor我不太清楚这意味着什么,我一直在谷歌上搜索教科书并且没有找到太多内容.任何人都可以帮助我或让我正确的方向来解决这个问题吗?

提前致谢!

Som*_*ude 11

定义了一个复制构造函数:

AssetList::AssetList(const AssetList&) : QList<Asset*>(){}
Run Code Online (Sandbox Code Playgroud)

但是你没有在课堂上宣布AssetList.

你需要添加它:

class AssetList : public QList<Asset*>
{
public:
    AssetList(){}
    ~AssetList();
    AssetList(const AssetList&);  // Declaring the copy-constructor

    ...
};
Run Code Online (Sandbox Code Playgroud)