【问题标题】:C++ - Candidate function not viable: no known conversion from 'struct ' to 'struct (&)'C++ - 候选函数不可行:没有已知的从 'struct' 到 'struct (&)' 的转换
【发布时间】:2018-10-08 17:01:48
【问题描述】:

首先,我是一个完整的 C++ 新手。我正在尝试使用一些结构数组(包括 2D 结构)作为参数调用函数,但出现以下错误:

没有用于调用“函数”的匹配函数:候选函数不可行:第一个参数没有从“struct _psoParticle [particleQnty][2]”到“_psoParticle (&)[][2]”的已知转换

我的结构(假设 material = 1 和 period = 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;
};

在 main 中初始化数据:

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);

然后弹出错误信息。

关于我做错了什么有什么想法吗?

谢谢!

【问题讨论】:

  • 实际的错误信息是什么?
  • @melpomene:没有匹配的函数调用'function'
  • 不,实际的完整错误消息。
  • 哦!我以前没看到!是 1. 候选函数不可行:需要单个参数“pso_CareUnit”,但提供了 8 个参数
  • 这还不是完整的信息。而你错过了minimal reproducible example

标签: c++ arrays function struct compiler-errors


【解决方案1】:

啊,是的,我知道这个……嗯,你的函数应该是这样的

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);

请注意,每个参数都有指定的完整维度。现在这样做的原因是你所有的数组都是 static 数组,所以编译器必须从一开始就知道它的大小,然后再运行你的程序。这样它就可以像 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::vector 这样的 stl 类型

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++ 指南以获取样式提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2021-04-21
    • 2023-03-16
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多