0

I'm trying to do the simply task of passing a vector to a function as a reference, and the modify the vector within the function. However, I get some pretty incomprehensible errors with the following example:

// data.h
class data {
public:
  std::string a;
  double b;
  uint8_t c;
};

We make a vector of these data objects in the MainWindow class:

// MainWindow.h
class MainWindow {
public:
  std::vector<data> vec;
  void needs_vec(std::vector<data> &vec);
};

Below I've got the troublemaker function, along with the constructor of the MainWindow object which is where everything comes together.

// other_file.h
void needs_vec(std::vector<data> &vec) {
  for (auto &vec: vec) {
    vec.b = get_a_number();
  }
};

// MainWindow.cpp
MainWindow::MainWindow() {
  // Read a configuration file and initialize the vector
  this->vec = read_config();

  // Try to apply our function
  needs_vec(this->vec);

  // Continue doing other things
};

I've also tried playing around with rvalue references but I meet similar results: an indecipherable twenty page error message.

nov1943
  • 31
  • 4

1 Answers1

1

Your class data doesn't have a double value called double, but b.

Apply these changes:

void MainWindow::needs_vec(std::vector<data> &vec) {
  for (auto &vec: vec) {      // Change the name here
    vec.b = get_a_number();
  }
}

Furthermore, are you closing your classes with a closing brace and semicolon, };?

E.g

// data.h
class data {
public:
  string a;
  double b;
  uint8_t c;
} /* including ";"?? */
Santiago Varela
  • 2,199
  • 16
  • 22