I'm writing some exception classes in c++ that inherit from a base class and I can't figure out why it won't compile. Any help would be appreciated.
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++ says:
In file included from RandomAccessFileNotFoundException.cpp:4:0: RandomAccessFileNotFoundException.h:13:1: error: expected class-name before ‘{’ token RandomAccessFileNotFoundException.cpp: In constructor ‘RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char*)’: RandomAccessFileNotFoundException.cpp:8:12: error: ‘m_message’ was not declared in this scope RandomAccessFileNotFoundException.cpp: In member function ‘const char* RandomAccessFileNotFoundException::getMessage()’: RandomAccessFileNotFoundException.cpp:14:12: error: ‘m_message’ was not declared in this scope
std::exceptionso that "catch-all" and such is much simpler for you later when you're catching via base class. Include<exception>to get the class.