1

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

1
  • Also, to make your life simpler, I'd suggest deriving all of your exceptions from std::exception so that "catch-all" and such is much simpler for you later when you're catching via base class. Include <exception> to get the class. Commented May 10, 2013 at 21:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@user2325753: I updated the answer. You have a typo in the name of the base class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.