Pregunta

I'm having issues with a Qt C++ Project that I'm working on at the moment. It's a new section that I'm covering and I'm finding it a bit confusing. I have created some classes Asset which is inherited by Stock, Bond and Savings classes. All this was okay. I then created a class called AssetList which derived QList, this class is where I have found the problem.

Here is the code I have so far.

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

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;
}

The error I'm getting at the moment is a compilation error: error: definition of implicitly declared copy constructor I'm not quite sure what this means, I've been googling around and looking in the textbook and haven't found much. Can anyone help me or put me in the right direction of figuring this out?

Thanks in advance!

¿Fue útil?

Solución

You define a copy constructor:

AssetList::AssetList(const AssetList&) : QList<Asset*>(){}

But you do not declare it in the AssetList class.

You need to add it:

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

    ...
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top