gpt4 book ai didi

c++ - 错误 : definition of implicitly declared copy constructor

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:35 26 4
gpt4 key购买 nike

我目前正在处理的 Qt C++ 项目有问题。这是我要介绍的一个新部分,但我发现它有点令人困惑。我创建了一些由股票、债券和储蓄类继承的 Assets 类。这一切都很好。然后我创建了一个名为 AssetList 的类,它派生了 QList,这个类是我发现问题的地方。

这是我目前的代码。

资源列表.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

Assets list .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::AssetList(const AssetList&) : QList<Asset*>(){}

但是您没有在 AssetList 类中声明它。

你需要添加它:

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

...
};

关于c++ - 错误 : definition of implicitly declared copy constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22760624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com