【发布时间】:2020-09-09 05:37:43
【问题描述】:
在下面的函数中,info 是一个包含getHDDList 函数的类,HDDList 是一个结构。但是当我将getHDDList 的输出分配给硬盘时,它会显示no operator "=" matches this operands:
HDDList* smc_getHDDList(Authorization* autho)
{
HDDList retVal;
//HDDList* hdd = new HDDList;
std::shared_ptr<info> inf (new info(autho));
std::shared_ptr<HDDList> hdd ;
try
{
//inf.get()->getHDDList();
//hdd = inf.get()->getHDDList();
}
catch (const std::invalid_argument& e)
{
THROW_EXCEPTION
}
return hdd;
}
这是我的HDDList 结构:
struct HDDList
{
const char* name;
double totalSize;
double freeSize;
double usedSize;
};
这是getHDDList函数:
HDDList* info::getHDDList()
{
int num;
num = std::stoi(numberOfHDDs());
HDDList* list = new HDDList[num];
for (int i = 0; i < num; i++)
{
list[i].name = listOfHDDs(i);
}
return list;
}
我使用了共享指针,因为当我在返回之前使用原始指针和删除指针时,我在接收结构时遇到了问题(它没有返回结构的第一个成员)。
如何将getHDDList 的输出分配给hdd?
【问题讨论】:
-
你能发一个minimal reproducible example吗?没有,就很难诊断您的问题。
-
指向指针的共享指针没有意义。并且为
HDDList对象使用共享指针也没有任何意义,因为您没有使用共享指针的任何所有权语义,而只是返回原始指针。简而言之,不要对hdd使用共享指针。实际上,我也不认为需要inf的共享指针,或者根本不需要使用指针。为什么不简单地return info(autho).getHDDList()? -
它工作正常。谢谢。但是如果我想将
info(autho).getHDDList()的输出放入hdd对象中然后返回hdd怎么办?它不应该是指针吗? @Someprogrammerdude -
为什么所有这些指针?
-
getHDDList的输出是一个名称列表。 @bipll
标签: c++ shared-ptr