结构没有在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:19:54  来源:igfitidea点击:

structure does not name a type in c++

c++structcompilation

提问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 typedefwithout 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_headeris declared inside of the Exampleclass, so you need to fully qualify its name in the implementation when it is a return type:

接下来,connection_headerExample类内部声明,因此当它是返回类型时,您需要在实现中完全限定其名称:

Example::connection_header Example::get_connection_header()

回答by masoud

Try below code in cppfile, add Example::before connection_header:

cpp文件中尝试以下代码,在Example::之前添加connection_header

Example::connection_header Example::get_connection_header() {
    return NULL;
}

connection_headeris defined inside Exampleso you should give it its definition scope.

connection_header是在里面定义的,Example所以你应该给它定义范围。

Also, the keyword typedefwill be ignored in C++. You can omit it

此外,该关键字typedef在 C++ 中将被忽略。你可以省略它

回答by Basile Starynkevitch

First, in C++ (but not C) every structor classnames a type. So if you declare a struct connection_header, you also get a connection_headertype, so you can later declare connection_header varsome variable.

首先,在 C++(但不是 C)中,每个structclass命名一个类型。所以如果你声明 a struct connection_header,你也会得到一个connection_header类型,所以你以后可以声明connection_header var一些变量。

Then, typedefboth in C and C++ needs a type and a name. For example:

然后,typedef在 C 和 C++ 中都需要一个类型和一个名称。例如:

 typedef long my_number_type;

declares my_number_typeas 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 structfor connection_headerabove the Exampleclass.

除了其他人的答案,我建议您不要嵌套数据结构的定义。创建一个单独的外部structconnection_header上述Exampleclass

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 urland methodmembers of Exampleand have get_connection_header()update these values in-place, returning void.

这更容易调试,提供更好的可重用性,并且更容易预测auto. 否则,只需 makeurlmethodmembers ofExampleget_connection_header()就地更新这些值,返回void.