I am creating a class where I defined a Struct called “Room” which I declare as private in the header file. I have several public functions that require a “Room” as an argument. When I compile (in g++) I get an error saying:
Graph.h:42:17: error: "Room" has not been declared
Yet here lies the declaration (whole header file now):
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <string>
using namespace std;
class Graph {
public:
// destructor
~Graph();
// copy constructor
Graph(const Graph &v);
// assignment operator
Graph & operator = (const Graph &v);
//Create an empty graph with a potential
//size of num rooms.
Graph( int num );
//Input the form:
//int -- numRooms times
//(myNumber north east south west) -- numRooms times.
void input(istream & s);
//outputs the graph as a visual layout
void output(ostream & s) const;
//Recursively searches for an exit path.
void findPath( Room start );
//Moves room N E S or W
void move( Room &*room , String direction );
//inputs the starting location.
void inputStart( int start );
//Searches the easyDelete array for the room with the
//number "roomNumber" and returns a pointer to it.
const Room * findRoom( int roomNumber );
private:
struct Room
{
bool visted;
int myNumber;
Room *North;
Room *East;
Room *South;
Room *West;
};
int numRooms;
int _index;
int _start;
Room ** easyDelete;
string * escapePath;
Room * theWALL;
Room * safety;
};
#endif
Are you not allowed to use structs defined inside the header file as arguments? If so, what’s the workaround?
Thanks.
It compiles fine without the
private:header. Why do you have this? Is the struct declared inside of a class?EDIT
You have used
Roombefore you declare it:Also, you can’t return a
Roomobject through the public method you have declared, since outside code won’t know anything about it.You need to predeclare it before using it:
Or you could just move the second
privateup, above thepublicsection.