结构没有在 C++ 中命名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15379218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
structure does not name a type in c++
提问by Kathick
I get an issue while return type is structure
返回类型是结构时出现问题
Example.h
class Example {
private:
typedef struct connection_header {
string url;
string method;
};
static connection_header get_connection_header();
};
Example.cpp
connection_header Example::get_connection_header() {
return NULL;
}
I am getting 'connection_header' does not name a type
我正进入(状态 'connection_header' does not name a type
may i know why is this error
我可以知道为什么会出现这个错误吗
回答by juanchopanza
You are using typedef
without giving a name to the type. Just drop the typedef
, it is not needed here:
您正在使用typedef
而不给类型命名。只需删除typedef
,这里不需要它:
struct connection_header {
string url;
string method;
};
Next, connection_header
is declared inside of the Example
class, so you need to fully qualify its name in the implementation when it is a return type:
接下来,connection_header
在Example
类内部声明,因此当它是返回类型时,您需要在实现中完全限定其名称:
Example::connection_header Example::get_connection_header()
回答by masoud
Try below code in cpp
file, add Example::
before connection_header
:
在cpp
文件中尝试以下代码,在Example::
之前添加connection_header
:
Example::connection_header Example::get_connection_header() {
return NULL;
}
connection_header
is defined inside Example
so you should give it its definition scope.
connection_header
是在里面定义的,Example
所以你应该给它定义范围。
Also, the keyword typedef
will be ignored in C++. You can omit it
此外,该关键字typedef
在 C++ 中将被忽略。你可以省略它
回答by Basile Starynkevitch
First, in C++ (but not C) every struct
or class
names a type. So if you declare a struct connection_header
, you also get a connection_header
type, so you can later declare connection_header var
some variable.
首先,在 C++(但不是 C)中,每个struct
或class
命名一个类型。所以如果你声明 a struct connection_header
,你也会得到一个connection_header
类型,所以你以后可以声明connection_header var
一些变量。
Then, typedef
both in C and C++ needs a type and a name. For example:
然后,typedef
在 C 和 C++ 中都需要一个类型和一个名称。例如:
typedef long my_number_type;
declares my_number_type
as a synonym for long
声明my_number_type
为同义词long
So as others pointed out, drop the typedef
所以正如其他人指出的那样,放弃 typedef
回答by Adam Erickson
In addition to the answers of others, I recommend that you do not nest the definition of your data structures. Create a separate external struct
for connection_header
above the Example
class
.
除了其他人的答案,我建议您不要嵌套数据结构的定义。创建一个单独的外部struct
对connection_header
上述Example
class
。
Example.h
struct ConnectionHeader {
string url;
string method;
};
class Example {
private:
ConnectionHeader connection_header;
static ConnectionHeader get_connection_header();
};
This is much simpler to debug, offers better reusability, and makes it easier to predict the results of auto
. Otherwise, just make url
and method
members of Example
and have get_connection_header()
update these values in-place, returning void
.
这更容易调试,提供更好的可重用性,并且更容易预测auto
. 否则,只需 makeurl
和method
members ofExample
并get_connection_header()
就地更新这些值,返回void
.