4
我遇到了我目前正在处理的Qt C++项目的问题。这是我所覆盖的一个新的部分,我发现它有点混乱。我创建了一些由股票,债券和储蓄类继承的类资产。这一切都很好。然后我创建了一个名为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
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;
}
我目前得到的错误是编译错误:error: definition of implicitly declared copy constructor
我不太清楚这是什么意思,我我一直在搜索并查看教科书,但发现并不多。任何人都可以帮助我,或者让我找到正确的方向吗?
在此先感谢!
如果我刚刚从AssetList.h文件中删除该声明,我得到完全相同的错误:/或我误解你的意思。 – nickcorin
@nickcorin:您需要为类定义添加*声明:'AssetList(const AssetList&);' –
@nickcorin在您使用的头文件中没有复制构造函数的声明。您需要将其添加到课程中。 –