繁体   English   中英

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

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

我面临以下问题。 在文件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_;
};

} }

然后我在另一个命名空间中定义了一个名为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."){ }

} }

我尝试使用多个编译器编译此代码。 使用 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 给出了一个等效的错误,Visual Studio 2017 给出了以下错误:

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

代码在以下任一情况下都能完美编译:

  1. 在文件parameter_exception.cpp 中,我指定了基类初始值设定项( proj::exception::Exception )的完整路径,或者
  2. 在文件parameter_exception.cpp 中,我从基类初始值设定项( Exception )中删除命名空间,或者
  3. 在文件my_exception.h 中,我从std::exception删除了继承,或者
  4. 我以其他方式重命名我的命名空间exception

据我从我得到的不同错误中了解到,编译器希望在类std::exception而不是在命名空间exception找到一个名为Exception的成员,但我不明白为什么会发生这种情况。 此外,当我首先从头文件parameter_exception.h中的exception::Exception继承时,我本来希望编译器给我一个错误,但它没有。

有人可以向我解释原因吗?

先感谢您。

正如@molbdnilo 所暗示的那样,名称查找存在问题。 问题在于名称“异常”被用于命名空间异常和标准::异常结构。 我从您发布的代码中删除了代码和注释。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM