简体   繁体   English

当初始值设定项是基类名称时出现错误“初始值设定项未命名非静态数据成员或基类”

[英]Error 'initializer does not name a non-static data member or base class' when the initializer is the base class name

I am facing the following problem.我面临以下问题。 In the file my_exception.h I have defined my own exception class inheriting from std::exception :在文件my_exception.h 中,我定义了我自己的继承自std::exception的异常类:

// File "my_exception.h"
#include <exception>
#include <string>

namespace proj { namespace exception {

struct Exception : public std::exception {
    explicit Exception(const std::string& msg) noexcept : msg_(msg) { }

    inline const char* what() const noexcept override { return msg_.c_str(); }

private:
    std::string msg_;
};

} }

Then I defined a derived exception class called BadParameterAccess in another namespace, splitting the declaration and the implementation in a .h and a .cpp files, respectively:然后我在另一个命名空间中定义了一个名为BadParameterAccess的派生异常类,分别在.h.cpp文件中拆分声明和实现:

// File parameter_exception.h
#include "exception.h"

namespace proj { namespace parameter {

struct BadParameterAccess final : public exception::Exception
{
    BadParameterAccess() noexcept;
};

} }

// File parameter_exception.cpp
#include "parameter_exception.h"

namespace proj { namespace parameter {

BadParameterAccess::BadParameterAccess() noexcept
    : exception::Exception("[BadParameterAccess] parameter not set yet."){ }

} }

I tried to compile this code using several compilers.我尝试使用多个编译器编译此代码。 With clang 6.0 I get the following error:使用 clang 6.0 我收到以下错误:

parameter_exception.cpp:7:18: error: initializer 'Exception' does not name a non-static data member or base class; did you mean the base class 'Exception'?
    : exception::Exception("[BadParameterAccess] parameter not set yet."){ }
                 ^~~~~~~~~
                 Exception
./parameter_exception.h:11:35: note: base class 'exception::Exception' specified here
struct BadParameterAccess final : public exception::Exception
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~

g++ 7 gives an equivalent error, and Visual Studio 2017 gives the following: g++ 7 给出了一个等效的错误,Visual Studio 2017 给出了以下错误:

parameter_exception.cpp(8): error C2039: 'Exception': is not a member of 'std::exception'

The code compiles perfectly when either:代码在以下任一情况下都能完美编译:

  1. in the file parameter_exception.cpp I specify the full path for the base class initializer ( proj::exception::Exception ), or在文件parameter_exception.cpp 中,我指定了基类初始值设定项( proj::exception::Exception )的完整路径,或者
  2. in the file parameter_exception.cpp I remove the namespaces from the base class initializer ( Exception ), or在文件parameter_exception.cpp 中,我从基类初始值设定项( Exception )中删除命名空间,或者
  3. in the file my_exception.h I remove the inheritance from std::exception , or在文件my_exception.h 中,我从std::exception删除了继承,或者
  4. I rename my namespace exception in some other way.我以其他方式重命名我的命名空间exception

As far as I have understood from the different errors I got, the compiler expects to find a member called Exception inside the class std::exception rather than inside the namespace exception , but I don't understand why this happens.据我从我得到的不同错误中了解到,编译器希望在类std::exception而不是在命名空间exception找到一个名为Exception的成员,但我不明白为什么会发生这种情况。 Moreover, I would have expected the compiler to give me an error when I inherit from exception::Exception in the header file parameter_exception.h first, but it does not.此外,当我首先从头文件parameter_exception.h中的exception::Exception继承时,我本来希望编译器给我一个错误,但它没有。

Can someone explain me the reason?有人可以向我解释原因吗?

Thank you in advance.先感谢您。

as @molbdnilo suggests there is a problem of name lookup.正如@molbdnilo 所暗示的那样,名称查找存在问题。 the problem is whith the name "exception" beeing used for namespace exception and the standard::exception struct.问题在于名称“异常”被用于命名空间异常和标准::异常结构。 I removed code and comments from the code you posted.我从您发布的代码中删除了代码和注释。

namespace standard {
    struct exception{
        explicit exception() noexcept { }
    };
}
namespace exception {
    struct A: public standard::exception {
        explicit A() noexcept { }
    };
}
namespace parameter {
    struct BadParameterAccess final : public exception::A
    {
        //BadParameterAccess() noexcept : exception::A() { }; // KO
        BadParameterAccess() noexcept : ::exception::A() { }; // OK
    };
}

namespace standard1 {
    struct exception1{
        explicit exception1() noexcept { }
    };
}
namespace exception2 {
    struct A: public standard1::exception1 {
        explicit A() noexcept { }
    };
}
namespace parameter1 {
    struct BadParameterAccess1 final : public exception2::A
    {
        BadParameterAccess1() noexcept : exception2::A() { }; // OK
        //BadParameterAccess1() noexcept : ::exception2::A() { }; // OK
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 成员初始化程序“ SuperClass”未命名非静态数据成员或基类 - member initializer 'SuperClass' does not name a non-static data member or base class 成员初始值设定项不命名非静态数据成员 - Member initializer does not name a non-static data member 模板类和继承问题-“列表”未命名非静态数据成员或基类 - Issues with template classes and inheritance - 'List' does not name a non-static data member or base class 枚举不是类的非静态数据成员或基类 - enum is not a non-static data member or base class of class 在继承的基类的成员上使用指定初始化器 - Using Designated Initializer on member of inherited base class 错误:“函数”不是非静态数据成员或“类”的基类 - Error: “Function” is not a Non static data member or base class of “Class” 该类中已删除的析构函数显示为虚拟/直接基类或非静态数据成员的类型 - Deleted destructor in the class appeared as a virtual/direct base class or as a type of non-static data member 默认基类初始化器 - Default base class initializer 成员初始化程序列表和非静态数据成员上的默认成员初始值设定项之间的区别是什么? - What's the differences between member initializer list and default member initializer on non-static data member? 非静态数据成员初始值设定项中lambda函数的分段错误 - Segmentation fault for lambda function in non-static data member initializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM