结构没有在c ++中命名一个类型

Kat*_*ick 9 c++ struct compilation

我得到一个问题,而返回类型是结构

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;
}
Run Code Online (Sandbox Code Playgroud)

我正进入(状态 'connection_header' does not name a type

我可以知道为什么会出现这个错误

jua*_*nza 11

您正在使用typedef而不给该类型命名.只需放下typedef,这里不需要:

struct connection_header {
    string url;
    string method;
};
Run Code Online (Sandbox Code Playgroud)

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

Example::connection_header Example::get_connection_header()
Run Code Online (Sandbox Code Playgroud)

  • @Lundin对`class`*中的`struct`*使用C风格声明不向后兼容. (2认同)