gpt4 book ai didi

C++方法签名问题

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

我正在尝试调用下面的 addValues:

Obj *s = new Obj();
vector<tm> dates(SIZE);
vector<double> values[COUNT];
for (uint i = 0; i < COUNT; i++) {
values[i] = vector<double>(SIZE);
}
s->addValues(&dates, &values); // <- this is the error line

并且我已经定义了 addValues:

void addValues(vector<tm> *newDates, vector<double> (*newValues)[COUNT]);

准确的错误是:

no matching function for call to ‘Stock::addValues(std::vector<tm, std::allocator<tm> >*, std::vector<double, std::allocator<double> > (*)[5])’

我认为这个想法是我的方法签名不匹配。addValues 的正确签名是什么?

最佳答案

template <size_t N>
void addValues(vector<tm>* newDates, vector<double> (&newValues)[N]);

之所以可行,是因为它是一个模板。值N在编译时已知,因为您将值定义为数组:vector<double> values[COUNT] .由于编译器在编译时知道值的大小,因此能够替换 NCOUNT .

由于它是一个模板,您可以使用任何大小的数组调用此函数,不一定是 COUNT 个大小。

我还建议按照 Fred Nurk 的建议将 newDates 更改为引用。

template <size_t N>
void addValues(vector<tm>& newDates, vector<double> (&newValues)[N]);

关于C++方法签名问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4860162/

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