-2

I have a function called generate_all_paths, defined as such:

template <int size>
void generate_all_paths(vector<string> maze[][size], int x, int y) {
....
}

I am trying to call it in my main function as so:

int main() {
  string s;
  ifstream mazefile("maze.txt");

  if (!mazefile) {
      cout << "File not found. Please try again." << endl;
  }
  while (getline(mazefile, s)) {
      mazevec.push_back(s);
  }
  generate_all_paths(mazevec, 0, 1);
  return 0;
}

where mazevec is vector<string> mazevec;

But my IDE says that my call to generate_all_paths in main does not match the function definition. I'm a little confused why this is happening. mazevec is a vector string, so shouldn't the parameter data types match up?

  • Whoops!! You forgot to declare `mazevec`. That's gonna be a problem. – Lightness Races in Orbit Jun 06 '15 at 04:17
  • 5
    It looks like you're programming by guessing, taking random code from the internet that you don't understand and changing some words to produce random code that you still don't understand. It's not going to go well!! [I suggest reading a book](http://stackoverflow.com/q/388242/560648). – Lightness Races in Orbit Jun 06 '15 at 04:19
  • I'm not really guessing. I previously had `maze[][size]` as an array but it was suggested to me to use a vector instead. I'm just confused why it's giving me an error since `mazevec` is defined as a `vector`. So theoretically it should match up with what generate_all_paths takes in. – jot1988 Jun 06 '15 at 04:32
  • Include the actual code declaring `mazevec` , instead of saying "where mazevec is vector mazevec;". – M.M Jun 06 '15 at 04:59

2 Answers2

2

The mazevec you are passing to the function is a vector<string>. Your function definition indicates that it expects a 2D vector array. In your function prototype, change it to this:

void generate_all_paths(vector<string> maze, int x, int y);

This should work.

Nivetha
  • 698
  • 5
  • 17
  • A better option to go by reference is to declare it this way: void generate_all_path(vector &maze, int x, int y). But certainly not the way shown by the asker. – Nivetha Jun 06 '15 at 04:20
  • @Nivetha I do apologize, I'm very new to C++ and just learning as I go. Your help is appreciated though. I made your suggested changes and made it `void generate_all_path(vector &maze, int x, int y)`, but the IDE still says that the parameters are not of matching data types. I understand why maze should be passed by reference, I'm still just confused on why it wouldn't compile. – jot1988 Jun 06 '15 at 04:25
  • @jot1988 While defining your mazevec variable i hope you have defined it in this way : vector mazevec – Nivetha Jun 06 '15 at 04:34
  • @Nivetha Yup, that's now I defined it. – jot1988 Jun 06 '15 at 04:37
0

You'll have to pass an array but you have passed a variable that is not an array. So they are treated as two different functions. Please pass an array of type vector and try again.

pramesh
  • 1,914
  • 1
  • 19
  • 30