Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: definition of implicitly declared copy constructor

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!

like image 756
Nick Corin Avatar asked Mar 31 '14 11:03

Nick Corin


People also ask

Is there a default copy constructor in C++?

Default Copy Constructors: When a copy constructor is not defined, the C++ compiler automatically supplies with its self-generated constructor that copies the values of the object to the new object.

What is the meaning of copy constructor?

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.

Can copy constructor be virtual in C++?

No you can't, constructors can't be virtual.

Which options are valid for calling a copy constructor?

In C++, a Copy Constructor may be called for the following cases: 1) When an object of the class is returned by value. 2) When an object of the class is passed (to a function) by value as an argument. 3) When an object is constructed based on another object of the same class.


1 Answers

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

    ...
};
like image 179
Some programmer dude Avatar answered Sep 29 '22 07:09

Some programmer dude