C4275 warning in Visual Studio
I get this warning when compiling my code in VS2008
warning C4275: non dll-interface class 'std::runtime_error' used as base for dll-interface class 'MyException' 2> 开发者_如何学Python c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\stdexcept(148) : see declaration of 'std::runtime_error'
My class is defined as
class MyException : public std::runtime_error
MSDN: http://msdn.microsoft.com/en-us/library/3tdb471s.aspx
"An exported class [as in DLL] was derived from a class that was not exported [as in DLL]."
Apparently you are declaring MyException
to be exportable from a DLL (by using: __declspec(dllexport)
), while std::runtime_error
is not exportable. Consider if MyException
really needs to be exportable. However, if none of the issues listed on the above page apply to your specific case, then you can disregard that warning--just be aware of the issues.
I wound up here looking for an answer to the same problem. I had a custom exception derived from std::runtime_error and was exporting it from my dll.
For exceptions, it seems like the simplest solution is to NOT EXPORT the derived class. You can do this if you move your implementation from the source (.CPP) file into the header (.HPP). For me, this was trivial. I imagine that in most cases with exceptions this would be the case.
This is a "good thing" because the client actual compiles and links the implementation of your custom exception with their implementation of std::runtime_error. This is what you want and is, in fact, what the C4275 warning is trying to protect you from: a runtime incompatibility between the two std::runtime_exception object types.
精彩评论