[英]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:代码在以下任一情况下都能完美编译:
proj::exception::Exception
), orproj::exception::Exception
)的完整路径,或者Exception
), orException
)中删除命名空间,或者std::exception
, orstd::exception
删除了继承,或者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.