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