使用结构作为参数:错误:结构未定义
我正在创建一个类,其中定义了一个名为“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它可以在没有
private:
标头的情况下正常编译。你为什么有这个?结构体是在类内部声明的吗?编辑
您在声明之前已使用
Room
:此外,您无法通过声明的公共方法返回
Room
对象,因为外部代码对此一无所知。您需要在使用它之前预先声明它:
或者您可以将第二个
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: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:
Or you could just move the second
private
up, above thepublic
section.如果您使用嵌套结构作为包含类的方法的参数,则必须使用完全限定名称,例如
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.要么像这样向前声明:
或者只是在类的顶部使用它之前声明并实现它。
Either forward declare it like this :
Or just declare and implement it before you use it at the top of the class.
void move( Room &*room , String direction ); //this is not valid C++
您必须在使用任何类型之前声明它。前向声明就足够了,因为您在同一范围内定义
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 withinGraph
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.