inheritance of exception classes in c++ - c++

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

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.

Related

C++ redefinition of function

I have a base class and 3 separate derived classes. All of the .hpp files are structured the same.
The .hpp which doesn't work:
#ifndef CAESARCIPHER_HPP
#define CAESARCIPHER_HPP
#include "Cipher.hpp"
#include<string>
class CaesarCipher : public Cipher {
public:
CaesarCipher(Key key)
: Cipher{ key } {}
std::string getCipherTypeString() const override {}
};
#endif
The .cpp of it (haven't implemented anything yet):
#include "CaesarCipher.hpp"
#include "Cipher.hpp"
#include<iostream>
CaesarCipher::CaesarCipher(Key key)
: Cipher{ key } {}
std::string CaesarCipher::getCipherTypeString() const {
return "";
}
One .hpp that does work:
#ifndef ASCIICIPHER_HPP
#define ASCIICIPHER_HPP
#include "Cipher.hpp"
#include<string>
class AsciiCipher : public Cipher {
public:
AsciiCipher(Key key)
: Cipher{ key } {}
std::string getCipherTypeString() const override {}
};
#endif
The base.hpp class looks like this:
typedef uint64_t Key;
#ifndef BASE_HPP
#define BASE_HPP
#include <string>
class Cipher {
Key key_;
public:
Cipher(Key key){}
virtual std::string getCipherTypeString() const {}
};
#endif
Errors:
CaesarCipher.cpp:6:15: error: redefinition of 'CaesarCipher'
CaesarCipher::CaesarCipher(Key key)
^
./CaesarCipher.hpp:9:9: note: previous definition is here
CaesarCipher(Key key)
^
CaesarCipher.cpp:9:27: error: redefinition of 'getCipherTypeString'
std::string CaesarCipher::getCipherTypeString() const {
^
./CaesarCipher.hpp:12:21: note: previous definition is here
std::string getCipherTypeString() const override {}
^
The problem is that 2 of the derived classes work perfectly but for one I get the above mentioned error for all of the functions. They look all the same just with changed names etc.
You have to:
Define the type Key.
Provide the missing body for the definition of Base::Base(Key).
Add a return statement to all functions that return non-void.
That should fix the errors that you are getting.

Error: expected ‘)’ before ‘*’ token despite not include-loop

I have a class RenderMachine which includes RenderObject and vice versa. I know there have been tons of questions about this error, but the solution for this doesn't do anything.
They say the error is mostly because a include-loop but I don't have one because in the RenderObject header I only allocate memory for a pointer to RenderMachine and vice versa.
RenderObject.h
#pragma once
#include "RenderMachine.h"
class RenderObject
{
public:
RenderObject(RenderMachine* rm){}
};
RenderMachine.h
#pragma once
#include "RenderObject.h"
class RenderMachine
{
public:
void add(RenderObject* renderObject);
};
The exact error is
error: expected ‘)’ before ‘*’ token
RenderObject(RenderMachine* rm)
^
Edit:
#include "RenderMachine.h"
class RenderMachine;
class RenderObject : public sf::Drawable
{
private:
int renderId;
public:
RenderObject(RenderMachine* rm){ (*rm).add(*this); }
int getRenderId() const { return renderId; }
#include "RenderObject.h"
class RenderMachine
{
std::vector< std::vector<sf::Drawable*> > renderVector;
public:
void add(RenderObject* renderObject);
And RenderMachine.cpp
#include "RenderMachine.h"
void RenderMachine::add(RenderObject* renderObject)
{
renderVector[(*renderObject).getRenderId()].push_back(renderObject);
}
Your call to add in the constructor of RenderObject should be done when RenderMachine is known (when it's a complete type). This goes for all calls to an object of the other type that you now have in your header files. Like this:
// RenderObject.h
class RenderMachine;
class RenderObject {
public:
RenderObject(RenderMachine* rm);
};
// RenderMachine.h
class RenderObject;
class RenderMachine {
public:
void add(RenderObject* renderObject);
};
// RenderObject.cpp
RenderObject::RenderObject(RenderMachine* rm) {
rm->add(this);
}
// RenderMachine.cpp
void RenderMachine::add(RenderObject* ro) {
}
You have a classic chicken-and-egg problem. The compiler cannot parse one header without first parsing the other header, because the classes in the headers refer to each other.
The solution is to use a forward declaration like this:
#include "RenderMachine.h"
class RenderMachine; // forward declaration
class RenderObject
{
public:
RenderObject(RenderMachine* rm){}
};

circular class dependency within template member

#ifndef CLASSB
#define CLASSB
#include "ClassA.h"
namespace name {
class ClassB
{
public:
static Handle conn();
};
}
#endif
-
#include "ClassB.h"
Handle name::ClassB::conn()
{
return getHandle(ClassA::it().str());
}
-
#ifndef CLASSA
#define CLASSA
#include "ClassB.h"
namespace name {
class ClassA
{
public:
template <typename T>
T myFunc(const std::string&)
{
auto tmp = ClassB::conn();
}
};
}
#endif
Calling ClassB::conn() gives a compiler error which says that the class ClassB is not declared. When I forward declare it I get an error message about an incomplete type.
I can't move the template function to my .cpp files as it is a template function. So, how to fix this?
Just remove #include "ClassA.h" from class B's header and it should work. But there appear to be multiple compilation problems with your code so it's hard to say (missing function getHandle, missing it(), missing type Handle etc).

C++ member access into incomplete type although header is included

I'm running into a strange problem using forwards declarations. Here is the code:
The Torse class, torse.hpp
#ifndef _TORSE_
#define _TORSE_
class Animation;
enum frame_type;
class Torse : public Renderable
{
public:
const glm::vec3& getCurrentRotation(frame_type);
};
#endif
in torse.cpp:
#include "torse.hpp"
#include "animation.hpp"
const glm::vec3& Torse::getCurrentRotation(frame_type t)
{...}
Now in Animation class, animation.hpp
#ifndef __ANIMATION_H__
#define __ANIMATION_H__
class torse;
class frame {...};
class Animation {
public:
void generateInterpolation(torse& me);
};
#endif
in animation.cpp:
#include "animation.hpp"
#include "torse.hpp"
void Animation::generateInterpolation(torse &me)
{
...
f1.rot[j] = me.getCurrentRotation(f1.type)[j];
...
}
As you can see I'm sharing the enum frame_type and the classes Anmation and Torse But I feel Like I'm doing it right, as in animation.cpp it should know how torse is thanks to torse.hpp...
clang gives me this error:
src/animation.cpp:19:43: error: member access into incomplete type 'torse'
f1.rot[j] = me.getCurrentRotation(f1.type)[j];
^
Anyone have a clue?
You defined a type Torse, but clang complains about a type named torse.
Fix your case.

Class Type Redefinition C++

I have seen other people asking this question before, but the answers they received were unique to their programs and unfortunately do not help me.
Firstly, I have a shape class - split into .h and .cpp files
//Shape.h
#include <string>
using namespace std;
class Shape
{
private:
string mColor;
public:
Shape(const string& color); // constructor that sets the color instance value
string getColor() const; // a const member function that returns the obj's color val
virtual double area() const = 0;
virtual string toString() const = 0;
};
//Shape.cpp
#include "Shape.h"
using namespace std;
Shape::Shape(const string& color) : mColor(NULL) {
mColor = color;
}
string Shape::getColor() const
{
return mColor;
}
I keep getting an error in my Shape.h class that says 'Shape' : 'class' type redefinition.
Any idea why I might be getting this error?
add include guard to your header file
#ifndef SHAPE_H
#define SHAPE_H
// put your class declaration here
#endif
And the way you initialize member mColor is incorrect. You can't assign NULL to string type
Shape::Shape(const string& color) : mColor(color) {
}
Add virtual destructor to Shape class as it serves as a base with virtual functions.
Also, do NOT use using directive in header file.
It seems like you want to write an abstract base class here, but is there any other files you compiled but not showed here?
You must include “shape.h” twice more.
Just use macros to prevent this case.
PS:I guess Rectangle is base class of Square,and also inherited Shape.