[英]inheriting from a class declarations in c++
当你想从C ++继承一个类时,在下面的第一行声明std是不合法的吗?
#ifndef HWEXCEPTION_H
#define HWEXCEPTION_H
#include <stdexcept>
class HWException : public std::run_time_error
{
void testException(int num);
};
#endif
VS
using std::run_time_error
class MyClass : public run_time_error
假设您在顶部有#include。 我有std :: run_time_error的编译错误,但似乎没有通过第二种方式这样做,并想知道为什么。
error C2039: 'run_time_error' : is not a member of 'std'
'run_time_error' : base class undefined
1>main.cpp
error C2039: 'run_time_error' : is not a member of 'std'
error C2504: 'run_time_error' : base class undefined
两者都是合法的。 但假设这是在头文件中,您不应使用using指令版本,因为它将名称放在全局命名空间中,这可能会导致标头用户出现问题。
编辑:刚刚注意到您的类名错误:
#include <stdexcept>
class MyClass : public std::runtime_error {
};
是你需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.