c++ - C++中异常类的继承

标签 c++ inheritance

我正在用 C++ 编写一些继承自基类的异常类,但我不明白为什么它无法编译。任何帮助,将不胜感激。

基类: RandomAccessFileException.h

#ifndef RANDOMACCESSFILEEXCEPTION_H
#define RANDOMACCESSFILEEXCEPTION_H

class RandomAcessFileException
{
public:
    RandomAcessFileException();
    virtual const char* getMessage() = 0;
protected:
    char m_message[100];
};

#endif

派生类: 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: 错误:“{”标记前的预期类名 RandomAccessFileNotFoundException.cpp:在构造函数“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];
};

如果不提供定义,链接器将发出 Unresolved 引用错误。

关于c++ - C++中异常类的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491104/

相关文章:

c++ - friend 类如何访问嵌套类的私有(private)成员?

C++ 用宏替换函数

java - 如何实现抽象子类继承的方法?

c# - Cast 接口(interface)和类对象

Java:在跳过中间继承的父类(super class)时调用基父类(super class)方法

c++ - 使用 GCC/MinGW 创建代理 DLL

c++ - 语句 “(void)startGuardBegin;” 的作用是什么?

c++ - TeamViewer 如何以编程方式在 Windows 上模拟 Ctrl-Alt-Del?

c# - 在构造函数中初始化一个虚拟属性是错误的吗?

javascript - 我如何(并且应该)在 DOM 对象上创建额外的方法?