在c ++中继承异常类

时间:2013-05-10 21:31:13

标签: c++ inheritance

我正在用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:错误:'{'标记之前的预期类名 RandomAccessFileNotFoundException.cpp:在构造函数'RandomAccessFileNotFoundException :: RandomAccessFileNotFoundException(const char *)'中: RandomAccessFileNotFoundException.cpp:8:12:错误:未在此范围内声明'm_message' RandomAccessFileNotFoundException.cpp:在成员函数'const char * RandomAccessFileNotFoundException :: getMessage()'中: RandomAccessFileNotFoundException.cpp:14:12:错误:'m_message'未在此范围内声明

1 个答案:

答案 0 :(得分:1)

第一个问题:

你必须:

#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];
};

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