使用结构作为参数:错误:结构未定义

发布于 2024-12-20 12:10:48 字数 1706 浏览 3 评论 0原文

我正在创建一个类,其中定义了一个名为“Room”的结构,我在头文件中将其声明为私有。我有几个需要“房间”作为参数的公共函数。当我编译(在 g++ 中)时,我收到一条错误消息:

Graph.h:42:17: error: "Room" has not been declared

Yethere isn't theclarification (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 Define inside the header file as argument?如果是这样,解决方法是什么?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜巴黎 2024-12-27 12:10:48

它可以在没有 private: 标头的情况下正常编译。你为什么有这个?结构体是在类内部声明的吗?

编辑

您在声明之前已使用Room

const Room * findRoom( int roomNumber );

此外,您无法通过声明的公共方法返回Room对象,因为外部代码对此一无所知。

您需要在使用它之前预先声明它:

class Graph {

public:

struct Room;

const Room * findRoom( int roomNumber );

struct Room
{
    bool visted;
    int myNumber;

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

Room room;
};

int main (){

  Graph x;

  return 0;
}

或者您可以将第二个 private 向上移动到 public 部分上方。

It compiles fine without the private: header. Why do you have this? Is the struct declared inside of a class?

EDIT

You have used Room before you declare it:

const Room * findRoom( int roomNumber );

Also, you can't return a Room object through the public method you have declared, since outside code won't know anything about it.

You need to predeclare it before using it:

class Graph {

public:

struct Room;

const Room * findRoom( int roomNumber );

struct Room
{
    bool visted;
    int myNumber;

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

Room room;
};

int main (){

  Graph x;

  return 0;
}

Or you could just move the second private up, above the public section.

泪是无色的血 2024-12-27 12:10:48

如果您使用嵌套结构作为包含类的方法的参数,则必须使用完全限定名称,例如 void outerclass::mymethod(outerclass::room); 尝试一下。您可能也需要将其公开。

If you are using a nested struct as an argument to a mthod of the containing class, then you must use the fully-qualified name such as void outerclass::mymethod(outerclass::room); Try that. You may need to make it public too.

往昔成烟 2024-12-27 12:10:48
  1. 房间不能是私人的,因为您在公共成员函数中使用它。
  2. 要么像这样向前声明:

    结构房间;
    // 析构函数
    〜图();
    
  3. 或者只是在类的顶部使用它之前声明并实现它。

  4. void move( Room &*room , String 方向 ); //这不是有效的 C++
  1. Room cannot be private since you use it in your public member functions.
  2. Either forward declare it like this :

    struct Room;
    // destructor
    ~Graph();
    
  3. Or just declare and implement it before you use it at the top of the class.

  4. void move( Room &*room , String direction ); //this is not valid C++
无言温柔 2024-12-27 12:10:48

您必须在使用任何类型之前声明它。前向声明就足够了,因为您在同一范围内定义 Graph::Room。但是,由于无论如何您都必须定义它,因此我建议在首次使用它之前将其移动到某个点。

Graph 中将 Room 设为私有是完全合法的(但是,如果您的公共界面出现问题,那么这是否合理是值得怀疑的)。

附带说明:指向引用的指针不是有效类型(对指针的引用是!)。因此,您的 move 函数无效。

You have to declare any type before using it. A forward declaration suffices because you are defining Graph::Room in the same scope. However, since you have to define it anyway, I would suggest moving it up to some point before first using it.

Making Room private within Graph is perfectly legal (it is questionable if it is reasonable, though, if your public interface fumbles with it).

On a side note: Pointers to references are no valid types (references to pointers are!). Your move function is therefore invalid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文