2010-05-13 72 views
1

当您想从C++中的类继承时,在下面的第一行中声明std是否是非法的?继承自C++中的类声明

#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 

这是假设你已经在顶部#包括。我有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 
+1

+1是否可以使用'?'?当然,但这是一个合理的问题,不应该因为标点符号错误而被低估。 – andand 2010-05-13 19:19:02

+0

@Suma“在下面第一行中声明std是非法的”似乎是一个足够清晰的问题,即使它缺少一个问号。 – 2010-05-13 19:19:21

+0

如果第一个版本出现错误(最好是参见Neil的答案),那么这个错误不在你显示的代码中。如果你不能解决这个问题,我建议你把十行(必要的包括和所有的,除了你所看到的错误以外完全编译),我们可以将其粘贴到我们的编辑器中,并尝试自己成为一个新的问题,并要求帮帮我。 – sbi 2010-05-13 19:37:39

回答

4

两者都是合法的。但假设这是在一个头文件中,你不应该使用using指令版本,因为它将名称放在全局名称空间中,这可能会导致头部用户出现问题。

编辑:只注意到你的类名错误:

#include <stdexcept> 
class MyClass : public std::runtime_error { 
}; 

是你所需要的。

+0

是的,对不起,小白错误。 – Crystal 2010-05-13 20:15:55