我正在为我用 C++ 编写的一个软件进行异常处理。我遇到一个编译器错误(使用 g++ (GCC) 4.8.1/mingw32)我不明白,这是一个最小的例子:
#include <iostream>
#include <exception>
class Bad_Img_Load: public std::exception {
//public:
virtual const char* what() const throw(){
return "An image could not be loaded.";
}
};
int main () {
try{
throw Bad_Img_Load();
}catch (Bad_Img_Load& e){
std::cout << e.what() << '\n';
}
return 0;
}
错误是:
a.cpp: In function 'int main()':
a.cpp:6:22: error: 'virtual const char* Bad_Img_Load::what() const' is private
virtual const char* what() const throw(){
^
a.cpp:15:25: error: within this context
std::cout << e.what() << '\n';
^
请注意,如果我取消注释“公共(public):”行,那么它就可以正常工作。但是它继承的类“异常”将所有内容定义为公共(public)的。所以我不明白为什么会出现这个错误。
最佳答案
您继承的类是否所有成员都公开并不重要。如果您使用(在您的代码中隐式指定的)私有(private)访问说明符重载继承类的公共(public)函数,则重载是私有(private)的。从技术上讲,您可以说您也重载了访问说明符。
关于c++ - 为什么会发生此编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32301200/