【发布时间】:2014-12-30 08:29:05
【问题描述】:
我有一个这样定义的基类:
namespace yxs
{
class File
{
public:
File(std::string fileName);
virtual ~File();
bool isExists();
size_t size();
protected:
std::string fileName;
std::ifstream* inputStream;
std::ofstream* outputStream;
}
然后我创建了一个继承上述基类的子类:
namespace yxs
{
class InputFile : File
{
public:
InputFile(std::string fileName);
virtual ~InputFile();
};
}
在另一个不相关的类中,我实例化了子类并尝试调用该方法:isExists()
void yxs::Engine::checkFile()
{
bool isOK = this->inputFile->isExists(); // Error on compile in this line
if(!isOK)
{
printf("ERROR! File canot be opened! Please check whether the file exists or not.\n");
exit(1);
}
}
但是,应用程序无法编译。编译器给出了两条错误信息:
Engine.cpp:66:34: 'isExists' is a private member of 'yxs::File'
和:
Engine.cpp:66:17: Cannot cast 'yxs::InputFile' to its private base class 'yxs::File'
当我尝试调用size() 方法时也发生了这些错误。
为什么会发生这个错误?在继承的概念中,是不是可以从子类调用父类的方法?
【问题讨论】:
标签: c++ inheritance