zengod
zengod

Reputation: 1174

Why am I getting the C++ no known conversion error here?

This is the relevant code:

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


I create an offsets 2d array and pass it into the init function. But this gives the following error upon compilation:

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

How can I fix this?


After following one of the comments and changing to vector ...(H, vector...)... I have the following code altogether:

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

But I get the following error:

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) {                                                                                                                                                                                                                                                             
     ^                                                           

Seems like what I really needed was, assuming that the 2d array of 2d vectors is not going to work, was:

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

Upvotes: 0

Views: 401

Answers (1)

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

This std::vector<std::vector<double> > **offsets doesn't do what you think it does. This is a pointer to a pointer to a std::vector.... What you are likely looking for is pass by reference:

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

This fits how you are calling this function and how you use the object internally.

There is a pretty good overview of this here: What's the difference between passing by reference vs. passing by value? but I would highly recomend that you investigate a good c++ book here: The Definitive C++ Book Guide and List

Upvotes: 1

Related Questions