C++ - 候选函数不可行:没有从"结构"到"结构(&)"的已知转换
C++ - Candidate function not viable: no known conversion from 'struct ' to 'struct (&)'
首先,我是一个完整的C 新手。我试图将一些结构数组(包括2D元素(作为参数调用,我会收到以下错误:
无匹配函数呼叫"函数":候选函数不可行:从'struct _psoparticle [earthqnty] [2] [2]''到'_psoparticle(&([] [2]'的已知转换
我的结构(让我们假设材料= 1且周期= 10(:
struct unit{
int inventory[material][period];
int consumption[material][period];
int replenishment[material][period];
int planned[material][period];
int accPlanned[material][period];
int demandPull[material][period];
int costPenalty[material][period];
bool delivery[material][period];
int leadtime;
int inventoryControl[material][period];
double bufferSize[material][period];
double bufferLevel[material][period];
double bufferMgmt[material][period];
const double lbGamma{-100.0};
const double ubGamma{100.0};
};
struct _psoParticle{
double positionBest;
double velocityBest;
long pbest;
};
在主中初始化数据:
struct unit CareUnit[2]{};
struct unit centralStore{};
struct _psoParticle psoParticle_CareUnit[10][2];
struct _psoParticle psoParticle_CentralStore[10];
int totalConsumption[material]{}, totalInventory{}, totalLateness{};
int particleQnty{10};
int x[particleQnty]{};
功能标头:
int dbr(_psoParticle (&pso_CareUnit)[][2], _psoParticle (&pso_CentralStore)[],
int particle, unit (&CareUnit)[], unit ¢ralStore,
int (&totalConsumption)[1], int &totalInventory, int &totalLateness);
主要继续:
for (int i = 0; i < particleQnty; ++i)
x[i] = dbr(psoParticle_CareUnit, psoParticle_CentralStore, i, CareUnit,
centralStore, totalConsumption, totalInventory, totalLateness);
然后弹出错误消息。
关于我做错了什么的想法?
谢谢!
啊,是的,我知道这个。...好吧,您的功能应该看起来像
int dbr(_psoParticle (&pso_CareUnit)[10][2], _psoParticle (&pso_CentralStore)[10],
int particle, unit (&CareUnit)[2], unit ¢ralStore,
int (&totalConsumption)[material], int &totalInventory, int &totalLateness);
请注意,每个参数都指定了完整的维度。现在,这样做的原因是您的所有数组都是静态数组,因此编译器必须从一开始就知道它是从一开始就知道的,然后才能运行程序。这样它就可以做诸如 sizeof(arr)
和 for(T obj: arr){}
之类的花哨的铃铛和哨子。
现在,正如您可能已经注意到的那样,这种做事的方式是一团糟。您有一些选择可以使它变得更好。最低效率是将您的参数类型替换为模板。喜欢
template<typename T1, typename T2, etc...
int dbr(T1 &pso_CareUnit, T2 & pso_CentralStore...
然后,编译器将弄清楚您本身要喂的到底是什么。您也可以将EM作为指示传递,那么一些信息将会丢失,您必须以某种方式沿着尺寸传递,但是无论如何...
int dbr(_psoParticle ** pso_CareUnit, _psoParticle *pso_CentralStore,...
您也可以使用STD类型,例如STD :: vector
int dbr(std::vector<std::vector<_psoParticle>>& pso_CareUnit, std::vector<_psoParticle>& pso_CentralStore,...
您也可以将整个内容封装,例如
struct Container{
_psoParticle careUnit[10][2];
_psoParticle centralStore[10];
};
int dbr(Container & iContainer...
甚至更好的
class Pso{
public:
int dbr(...
private:
_psoParticle careUnit[10][2];
_psoParticle centralStore[10];
// ... the rest of arguments
};
还有一些更时髦的方法可以处理它,例如迭代器等。但是在您的情况下,我认为简单的指针解决方案或封装就足够了。尽管我要警告您将来不要使用C风格的结构和阵列,但它们很烦人,并且都有各种不错的STL容器。您的命名约定非常奇怪,请检查Google或GNU C 造型提示指南。
相关文章:
- 在C++中将结构转换和存储为二进制
- 如何大致将 cpp 代码结构转换为 python
- 如何将可变大小的结构转换为字符数组
- 尝试传递结构数组,但出现"无法将'结构'转换为'结构*'错误
- 我可以安全地将浮点数的结构转换为浮点数组吗C++?
- 如何将结构转换为字符串?
- 如何将C++结构转换为 C# 等效结构?
- 将结构转换为布尔值
- 将具有不同字段的结构转换为 uint8 的数组
- 将 constexpr 结构转换为运行时结构
- 将结构转换为类
- 我必须将记录结构转换为一类,并且不断遇到多个错误
- 如何将 ANSI C 结构转换为 C++ 类,但保持其 ANSI C 友好
- 将 C++ 中的结构转换为 Java 中的类
- 将结构转换为 constexpr 数组uint8_t
- 将大型结构转换为小型C
- c++正在将未知结构转换为字符串
- C++隐式将平凡可构造的结构转换为成员
- 将C++联合结构转换为 VB6
- C到C++:将结构转换为类