1

我正在用 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”在这方面没有申明

4

1 回答 1

1

第一个问题:

你必须:

#include "RandomAccessFileException.h"

在你的头文件中,因为它包含(即)RandomAccessFileNotFoundException.h的基类的定义。RandomAccessFileNotFoundExceptionRandomAccessFileException

所以总结一下,你的头文件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];
};

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

于 2013-05-10T21:32:05.100 回答