Reputation: 465
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
Upvotes: 1
Views: 263
Reputation: 126422
First problem:
You have to:
#include "RandomAccessFileException.h"
In your RandomAccessFileNotFoundException.h
header file, since it contains the definition of the base class of RandomAccessFileNotFoundException
(i.e. RandomAccessFileException
).
So to sum it up, your header file RandomAccessFileNotFoundException.h
header should be:
#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
Second problem:
Also notice that you have a typo. Your base class is called:
RandomAcessFileException
// ^
Instead of:
RandomAccessFileException
// ^^
Which is the name you are using in RandomAccessFileException.h
.
Third problem:
Finally, you are missing a definition of the base class's (RandomAccessFile
) constructor, for which you have provided just a declaration:
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];
};
Without providing a definition, the linker will emit an unresolved reference error.
Upvotes: 1