C++ - 候选函数不可行:没有从"结构"到"结构(&)"的已知转换

C++ - Candidate function not viable: no known conversion from 'struct ' to 'struct (&)'

本文关键字:结构 转换 不可行 C++ 候选函数      更新时间:2023-10-16

首先,我是一个完整的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 &centralStore,
            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 &centralStore,
        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 造型提示指南。