gpt4 book ai didi

c++ - 为什么我在这里得到 C++ no known conversion error?

转载 作者:行者123 更新时间:2023-11-28 04:07:43 25 4
gpt4 key购买 nike

这是相关代码:

void init(int f, int h, std::vector<std::vector<double> > **offsets) {                                                                                                                                                                                                                                                        
for (int i = 0; i < 2 * h + 1; i++) { // for all frames
// vector of (x, y, d)'s
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
random_offsets(offsets[y][x]);
print_vecs(offsets[y][x]);
}
}
}
}


int main(int argc, char **argv) {

int h = 6;
Halide::Runtime::Buffer<uint8_t> frame[2*h + 1];
int f = 6;
for (int i = -h; i < h; i++) {
frame[i] = load_image("test1/small/" + std::to_string(f + i) + ".png");
}

W = frame[0].width();
H = frame[0].height();

std::vector<std::vector<double> > offsets[H][W];
init(6, h, offsets);
return 0;
}


我创建了一个 offsets 二维数组并将其传递给 init 函数。但这在编译时会出现以下错误:

note: candidate function not viable: no known conversion from 'std::vector<std::vector<double> > [H][W]' to                                                                                                                                                                                                    
'std::vector<std::vector<double> > **' for 3rd argument
void init(int f, int h, std::vector<std::vector<double> > **offsets) {

我该如何解决这个问题?


在关注其中一条评论并更改为 vector ...(H, vector...)...我总共有以下代码:

void random_offsets(std::vector<std::vector<double> > &offsets) {                                                                                                                                                                                                                                                             
for (int i = 0; i < k; i++) {
std::vector<double> offset;
offset.push_back(W/3 * ((double) rand() / (RAND_MAX)) * 2 - 1);
offset.push_back(W/3 * ((double) rand() / (RAND_MAX)) * 2 - 1);
offsets.push_back(offset);
}
}


void init(int f, int h, std::vector<std::vector<double> > &offsets) {
for (int i = 0; i < 2 * h + 1; i++) { // for all frames
// vector of (x, y, d)'s
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
random_offsets(offsets[y][x]);
print_vecs(offsets[y][x]);
}
}
}
}


int main(int argc, char **argv) {

int h = 6;
Halide::Runtime::Buffer<uint8_t> frame[2*h + 1];
int f = 6;
for (int i = -h; i < h; i++) {
frame[i] = load_image("test1/small/" + std::to_string(f + i) + ".png");
}

W = frame[0].width();
H = frame[0].height();

std::vector<std::vector<double>> offsets(H, std::vector<double>(W));
init(6, h, offsets);
return 0;
}

但是我得到以下错误:

init.cpp:43:2: error: no matching function for call to 'random_offsets'                                                                                                                                                                                                                                                       
random_offsets(offsets[y][x]);
^~~~~~~~~~~~~~
init.cpp:14:6: note: candidate function not viable: no known conversion from 'std::__1::__vector_base<double, std::__1::allocator<double> >::value_type'
(aka 'double') to 'std::vector<std::vector<double> > &' for 1st argument
void random_offsets(std::vector<std::vector<double> > &offsets) {
^

似乎我真正需要的是,假设二维 vector 的二维数组不起作用,是:

std::vector<std::vector<std::vector<std::vector<double> > > > offsets(H, std::vector<std::vector<std::vector<double> > > (W, std::vector<std::vector<double> > (k, std::vector<double> (3)))); 

最佳答案

std::vector<std::vector<double> > **offsets不做你认为它做的事。这是一个指向 std::vector... 的指针.您可能正在寻找的是通过引用传递:

void init(int f, int h, std::vector<std::vector<double> > &offsets)

这符合您调用此函数的方式以及您在内部使用该对象的方式。

这里有一个很好的概述:What's the difference between passing by reference vs. passing by value?但我强烈建议您在这里研究一本很好的 C++ 书籍:The Definitive C++ Book Guide and List

关于c++ - 为什么我在这里得到 C++ no known conversion error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58424539/

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