c++中异常类的继承

inheritance of exception classes in c++

本文关键字:继承 异常 c++      更新时间:2023-10-16

我正在用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];
};

如果不提供定义,链接器将发出一个无法解析的引用错误。