c++中异常类的继承
inheritance of exception classes in c++
我正在用c++编写一些从基类继承的异常类,我不知道为什么它不会编译。任何帮助都会很感激。
Base Class:
RandomAccessFileException.h
#ifndef RANDOMACCESSFILEEXCEPTION_H
#define RANDOMACCESSFILEEXCEPTION_H
class RandomAcessFileException
{
public:
RandomAcessFileException();
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};
#endif
Derived Class:
RandomAccessFileNotFoundException.h
#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#include "RandomAccessFileException.h"
class RandomAccessFileNotFoundException : public RandomAccessFileException
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};
#endif
RandomAccessFileNotFoundException.cpp
#include <cstring>
#include "RandomAccessFileException.h"
#include "RandomAccessFileNotFoundException.h"
RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char* p_filename)
{
strcat(m_message, "RandomAccessFileNotFoundException: File: ");
strcat(m_message, p_filename);
}
const char* RandomAccessFileNotFoundException::getMessage()
{
return m_message;
}
g++说:
包含在RandomAccessFileNotFoundException.cpp:4:0的文件中:RandomAccessFileNotFoundException.h:13:1:错误:期望类名在{token之前在构造函数RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char*):RandomAccessFileNotFoundException.cpp:8:12:错误:' m_message '未在此范围内声明RandomAccessFileNotFoundException.cpp:在成员函数' const char* RandomAccessFileNotFoundException::getMessage() '中:RandomAccessFileNotFoundException.cpp:14:12:错误:' m_message '未在此范围内声明
第一个问题:
你必须:
#include "RandomAccessFileException.h"
在你的RandomAccessFileNotFoundException.h
头文件中,因为它包含了RandomAccessFileNotFoundException
(即RandomAccessFileException
)的基类的定义。
RandomAccessFileNotFoundException.h
头应该是:
#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#include "RandomAccessFileException.h"
class RandomAccessFileNotFoundException : public RandomAccessFileException
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// This class is defined in the
// RandomAccessFileException.h
// header, so you have to #include
// that header before using this
// class as a base class.
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};
#endif
第二个问题:
还要注意你有一个错别字。你的基类被称为:
RandomAcessFileException
// ^
代替:
RandomAccessFileException
// ^^
您在RandomAccessFileException.h
中使用的名称。
第三个问题:
最后,您缺少了基类(RandomAccessFile
)构造函数的定义,为此您只提供了一个声明:
class RandomAcessFileException
{
public:
RandomAcessFileException();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This is a DECLARATION of the constructor, but the definition is missing
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};
如果不提供定义,链接器将发出一个无法解析的引用错误。
- C++中模板化异常类的多重继承
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- 关于异常继承中的语法的问题
- 无法创建两个从 std::logic_error 继承的自定义异常类
- 如何在继承的类中没有构造函数时引发异常
- 异常类的多重继承
- C++通过继承 std::exception 来创建新的异常
- 创建继承自C++中io类的异常类
- 模板多重继承异常符号错误
- 为什么异常捕获是基于顺序的,而不是基于最接近继承的
- C++异常继承模糊性
- 从boost::exception和std::runtime_error继承自定义异常类
- C++ 继承另一个库中的库异常,以将它们传递给调用代码
- 使用继承和运算符重载或异常处理的通讯簿
- C++和并发编程中的继承异常
- c++中异常类的继承
- 异常继承
- 你能把一个异常继承层次结构包装到另一个吗?——或者,另一种干净的处理方式
- 异常是否需要虚拟继承?
- 为什么继承影响异常处理