没有命名类型错误

时间:2011-12-11 03:51:46

标签: c++

这是我遇到的最愚蠢的小问题,我找不到原因。

我知道已经有几十个这样的错误已经被问过了,但是我读到的每一个都是关于声明的顺序,但是,在我在函数中使用它之前我声明了我的结构,并且仍然得到错误。

这是头文件:

#ifndef GRAPH_H
#define GRAPH_H


#include <iostream>
#include <string>
using namespace std;

class Graph {

    public:
    struct Room;

    // 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 , string str );

    //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.
    Room * findRoom( int roomNumber );

    struct Room
    {
        bool visited;
        int myNumber;

        Room *North;
        Room *East;
        Room *South;
        Room *West;
    };

    private:

    int numRooms;
    int _index;
    int _start;

    Room ** easyDelete;
    string * escapePath;

    Room * theWALL;
    Room * safety;
};

#endif

具体错误是:错误:“房间”没有命名类型,它正在谈论我的

Room * findRoom( int roomNumber );

函数,它应该返回指向“Room”的指针。 我试过将结构的实际定义放在“struct Room”中无济于事。

编辑:道歉,我使用它时在.C文件中:

#include <iostream>
#include <string>
#include "Graph.h"
using namespace std;

...

Room * Graph::findRoom( int roomNumber )
{
    ...
}

1 个答案:

答案 0 :(得分:5)

你确定错误指向这个.h文件吗?不应该有这个错误...

如果你像这样编写实现(.cpp)

Room * Graph::findRoom( int roomNumber );

它应该抱怨,因为Room是Graph的一部分:

Graph::Room * Graph::findRoom( int roomNumber );