Help with returning an object

I am creating a maze game where the load function takes a file that contains a maze and returns a maze object which contains the actual maze(which is a 2d vector) and start and finish positions. The compiler does not like how I am attempting to return the maze object, I have a 2d vector created as well as the start and finish locations, I believe I am just simply screwing up the object declaration and/or the return statement.

Here is the Maze class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  class Maze;

Maze& load(std::string filename);

class Maze {
	friend std::ostream &operator <<(std::ostream &os, Maze &maze);
	friend Maze& load(std::string filename);
public:
    Maze(std::vector<std::vector<bool> > spec, const Location &start, const Location &end);
    bool solve();
    //void run();
private:
	bool contains(const Location &location) const;
	bool isPath(const Location &location) const;
	int height() {return spec.size();}
	int width() {return spec[0].size();}
	std::vector<std::vector<bool> > spec;
	const Location start, finish;
	Location current;
};


the load function, I'm only showing the necessary parts as the loading of the vector and reading of file works fine.
1
2
3
4
5
6
7
8
9
10
Maze& load (string filename) {
vector<vector<bool> > mazeSpec;

cout << mazeSpec.at(11).at(4) << endl; //location of start test to make sure true
cout << mazeSpec.at(2).at(29) << endl; //location of finish test to make sure true
const Location start(11,4); //start position
const Location finish(2, 29); //finish position
Maze maze(mazeSpec, start, finish);
return maze;
}


Any help would be appreciated.
maze is a local variable inside the load function so it will stop to exist when the function returns. The return type is a reference type so what you get when you call the load function is a reference to a no longer existing Maze object. What you probably want to do is to change the return type to Maze (not a reference).
you're right, I knew it was simple. thanks.
Topic archived. No new replies allowed.