one of my class is already declaring the variable to open the file, now that i need to called it in main class. I couldn't find a way to do so, can someone please give me some hint to call it.

Here is what it declare in the class.

Maze::Maze(const string& fileName):
            maze(0),   // init maze
            rows(0),
            cols(0),
            mouseRow(0),
            mouseCol(0),
            cheeseRow(0),
            cheeseCol(0),
            squaresVisited(0)
{
    // declare variable to open file etc.

	ifstream inmaze(fileName.c_str());
    if (inmaze == 0) 
	{
        cerr << "Couldn't open maze file " << maze_file << endl;
        return;
        }
.......

in main

int main()
{
 // i tried to call the maze class, but it said there is no default constructor
}

thx in advance

Recommended Answers

All 3 Replies

Is the class you posted really what you have on your computer??? Or did you just type it into your post from memory?

Use copy/paste to copy the program into the clipboard then paste it into your post. What you posted above is useless to us.

Thanks for quick reply

umm, I just copy part of the code
here is my complete project...
maze.h

#include <string>
#include <vector>

using namespace std;
typedef struct
{
    short int doorEncoding; // Range 0..15.
	bool north;
	bool east;
	bool south;
	bool west;

            // Note that the result of these operations is not
            // necessarily 1, but some non-zero value if there
            // is a door in that particular direction. 0 else.
    bool      visited;
    int       parentRow;
    int       parentCol;
    char      direction; // From parent.
} MazeCell;

typedef struct
{
    int row;
    int col;
} CellPosition; // useful as StackItem or QueueItem. 


class Maze
{
public:
    Maze(const string& fileName); // Load from file.
    void Solve(); // Solve the maze.
    void PrintSolution();
private:
    vector<vector<MazeCell> > maze; // maze square
    int rows;                        // number of rows
    int cols;                        // number of columns
    int mouseRow;                    // row position of mouse
    int mouseCol;                    // col position of mouse
    int cheeseRow;                   // row position of cheese
    int cheeseCol;                   // col position of cheese
    int squaresVisited; 
};

and here implementation

#include "Maze.h"
#include <iostream>
#include <fstream>
#include <Stack>
#include <Queue>

using namespace std;

Maze::Maze(const string& fileName):
            maze(0),   // init maze
            rows(0),
            cols(0),
            mouseRow(0),
            mouseCol(0),
            cheeseRow(0),
            cheeseCol(0),
            squaresVisited(0)
{
    // declare variable to open file etc.

	ifstream inmaze(fileName.c_str());
    if (inmaze == 0) 
	{
        cerr << "Couldn't open maze file " << maze_file << endl;
        return;
    }
    // read m and n and init rows and cols.

        inmaze >> rows >> cols;

        // read and init mouse and cheese positions.

		inmaze >> mouseRow >> mouseCol;
		inmaze >> cheeseRow >> cheeseCol;

    // Now we know the size of maze. let us init.
    // Reserve space for rows (= n) many rows.
    maze.resize(rows);
    // reserve space for cols (= n) many columns in each row.
    for (int rowNum = 0; rowNum < rows; rowNum++)
    {
        maze[rowNum].resize(cols);
    }
    // Now we can use maze[0][0] ... maze[n-1][m-1].
    // Read and initialize all the cells.

	for(int r = 0; r < rows; r++)
		for(int c =0; c < cols; c++)
		{
			int temp;
			temp = maze[r][c].doorEncoding; //set doorEncoding to temp
			if(temp >= 8) //if temp bigger than 8
			{
				maze[r][c].north = true;
				temp -= 8; 
			}
			if(temp >= 4)
			{
				maze[r][c].east = true;
				temp -= 4;				
			}
			if(temp >= 2)
			{
				maze[r][c].south = true;
				temp -= 2;
			}
			if(temp >= 1)
			{
				maze[r][c].west = true;
				temp -= 1;
			}
				
		}
};

	void Maze::Solve()
	{
		CellPosition cp;
		stack<CellPosition> stk;
		maze[mouseRow][mouseCol].visited = true;
		maze[mouseRow][mouseCol].parentRow = -1;
		maze[mouseRow][mouseCol].parentCol = -1;
		cp.row = mouseRow;
		cp.col = mouseCol;
		stk.push(cp);
		bool solved;
		
		while(solved == false)
		{
			if(maze[mouseRow][mouseCol].north == true) //if north door is open
			{
				maze[mouseRow][mouseCol] = maze[mouseRow][mouseCol -1]; //current pos move up one
				maze[mouseRow][mouseCol].visited = true;
				maze[mouseRow][mouseCol].direction = 's';
				maze[mouseRow][mouseCol].parentRow = mouseRow;
				maze[mouseRow][mouseCol].parentCol = mouseCol +1;
				cp.row = mouseRow;
				cp.col = mouseCol;
				stk.push(cp);
				if(maze[mouseRow][mouseCol].north ==false
					maze[mouseRow][mouseCol].east == false
					maze[mouseRow][mouseCol].west == false)
				{
					stk.pop();
				}
				
			}
			else if(maze[mouseRow][mouseCol].east == true) //if east door is open
			{
				maze[mouseRow][mouseCol] = maze[mouseRow +1][mouseCol]; //current pos move to the right one
				maze[mouseRow][mouseCol].visited = true;
				maze[mouseRow][mouseCol].direction = 'w';
				maze[mouseRow][mouseCol].parentRow = mouseRow -1;
				maze[mouseRow][mouseCol].parentCol = mouseCol;
				cp.row = mouseRow;
				cp.col = mouseCol;
				stk.push(cp);
			}
			else if(maze[mouseRow][mouseCol].south == true)
			{
				maze[mouseRow][mouseCol] = maze[mouseRow][mouseCol+1];
				maze[mouseRow][mouseCol].visited = true;
				maze[mouseRow][mouseCol].direction = 'n';
				maze[mouseRow][mouseCol].parentRow = mouseRow;
				maze[mouseRow][mouseCol].parentCol = mouseCol;
				cp.row = mouseRow;
				cp.col = mouseCol;
				stk.push(cp);
			}
			else if(maze[mouseRow][mouseCol].west == true)
			{
				maze[mouseRow][mouseCol] = maze[mouseRow -1][mouseCol];
				maze[mouseRow][mouseCol].visited = true;
				maze[mouseRow][mouseCol].direction = 'e';
				maze[mouseRow][mouseCol].parentRow = mouseRow;
				maze[mouseRow][mouseCol].parentCol = mouseCol;
				cp.row = mouseRow;
				cp.col = mouseCol;
				stk.push(cp);
			}
			else
				cout << "No Solution!" << endl;
				
			if(maze[mouseRow][mouseCol].visited == true)
			{
				solved = true;
			}
		}

	
	};
/*
	void Maze::Solve()
	{
		Queue<CellPosition>;
	}*/
#include "Maze.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{	
}

Basically I try to solve a maze using the input file and solve it by stack and queue. But I don't know how to open the input text using declare variable and open it in main.
not sure this is enough info...

nvm, I found the solution to that. But if anyone(if i can continue this post), please help me with the stack and queue for this maze. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.