Template class and derived classes - c++

I have templated MyDatabase class and two derived classes Database_A and Database_B. I have another class C which is using Database_A and Database_B. So, whenever I am including headers of both derived class (Database_A and Database_B) in class C, it gives me this error "Redefinition of MyDatabase". My guessing is, because both derived class (Database_A and Database_B) are dragging template class MyDatabase, it could be the reason for redefinition of MyDatabase. But I don't know how to resolve this problem.
My code:
template <typename U, class T> class My_DataBase {
protected:
std::map<U,T> Container
public:
void add();
T* getNameToPointer(string key);
};
class Database_A: public My_Database <string,A> {
add();
A* getNameToPointer(name);
};
class Database_B: public My_DataBase <string,B> {
add();
B* getNameToPointer(name);
};
class C {
private:
Database_A Db_A; // drage template class on back_end
Database_B Db_B; // drage template class on back_end
};
How can I solve this problem?

It seems like you have two header files, where each header file contains the base class and a dereived class. That's why you get a redefinition error.
To avoid this issue do this: Use three header files and include guards. The base class will be in one header file, named "My_DataBase.h". The two other headers files (one for every dereived class) will include "My_DataBase.h".
However, since you may want to include both header files (of the derived classes), you need to make use of the include guards to avoid redefinition of the base class.
So here is an example:
My_DataBase.h
#ifndef MY_DATABASE_H
#define MY_DATABASE_H
template <typename U, class T> class My_DataBase {
protected:
std::map<U,T> Container
public:
void add();
T* getNameToPointer(string key);
}
#endif
Database_A.h
#ifndef DATABASE_A_H
#define DATABASE_A_H
#include "My_DataBase.h"
class Database_A: public My_Database <string,A> {
add();
A* getNameToPointer(name);
}
Database_B.h
#ifndef DATABASE_B_H
#define DATABASE_B_H
#include "My_DataBase.h"
class Database_B:public My_DataBase <string,B> {
add();
B* getNameToPointer(name);
}

You should prevent header files from multiple inclusion.
You can do this either by specifiying #pragma once instruction on the top of your header files (however I think this is Microsoft specific).
A better way is to surround your header file by code similar to the following :
#ifndef MYHEADER_FILENAME
#define MYHEADER_FILENAME
/// whatever contained in your header file
...
#endif // MYHEADER_FILENAME

Related

Is it reasonable to put all typedefs and forward declarations in one header file in a mid-size project

There are many good discussions about #include, but finally I did not find an answer on the specific question:
Is it reasonable to put forward declarations for all classes and all typedefs into one header file for a mid-size project?
So I could avoid to explicitly making forward definitions at the beginning of each header, and I would automatically have all types available.
Some remarks: I do not use namespaces currently, but that will probably be the next step. Mid-size means, it's maintained mostly by one person and has up to 10000 lines of code / 100 classes. Most of the classes are templates and exist only in a header file.
Structure could then look:
types.h:
class A;
template <class T> class B;
template <class T> class C;
typedef C<int> intC;
A.h:
#include "types.h"
class A{
public:
do_something(intC c);
}
B.h:
#include "types.h"
template <class T> class B{
public:
B(A* a, T val){ ... };
}

How to avoid #include dependency to external library

If I'm creating a static library with a header file such as this:
// Myfile.h
#include "SomeHeaderFile.h" // External library
Class MyClass
{
// My code
};
Within my own project I can tell the compiler (in my case, Visual Studio) where to look for SomeHeaderFile.h. However, I don't want my users to be concerned with this - they should be able to include my header without having to inform their compiler about the location of SomeHeaderFile.h.
How is this type of situation normally handled?
This is a classic "compilation firewall" scenario. There are two simple solutions to do:
Forward-declare any classes or functions that you need from the external library. And then include the external library's header file only within your cpp file (when you actually need to use the classes or functions that you forward-declared in your header).
Use the PImpl idiom (or Cheshire Cat) where you forward-declare an "implementation" class that you declare and define only privately (in the cpp file). You use that private class to put all the external-library-dependent code to avoid having any traces of it in your public class (the one declared in your header file).
Here is an example using the first option:
#ifndef MY_LIB_MY_HEADER_H
#define MY_LIB_MY_HEADER_H
class some_external_class; // forward-declare external dependency.
class my_class {
public:
// ...
void someFunction(some_external_class& aRef); // declare members using the forward-declared incomplete type.
};
#endif
// in the cpp file:
#include "my_header.h"
#include "some_external_header.h"
void my_class::someFunction(some_external_class& aRef) {
// here, you can use all that you want from some_external_class.
};
Here is an example of option 2:
#ifndef MY_LIB_MY_HEADER_H
#define MY_LIB_MY_HEADER_H
class my_class_impl; // forward-declare private "implementation" class.
class my_class {
private:
std::unique_ptr<my_class_impl> pimpl; // a vanishing facade...
public:
// ...
};
#endif
// in the cpp file:
#include "my_header.h"
#include "some_external_header.h"
class my_class_impl {
private:
some_external_class obj;
// ...
public:
// some functions ...
};
my_class::my_class() : pimpl(new my_class_impl()) { };
Say the external header file contains the following:
external.h
class foo
{
public:
foo();
};
And in your library you use foo:
myheader.h:
#include "external.h"
class bar
{
...
private:
foo* _x;
};
To get your code to compile, all you have to do is to forward declare the foo class (after that you can remove the include):
class foo;
class bar
{
...
private:
foo* _x;
};
You would then have to include external.h in your source file.

Circular Class Dependency with Includes

Here is basically what my class layout is:
GameDriver.h:
#ifndef GAMEDRIVER_H
#define GAMEDRIVER_H
#include "CameraSystem.h"
class CameraSystem; //Forward declaration
class GameDriver {
//Stuff
};
#endif
CameraSystem.h:
#ifndef CAMERASYSTEM_H
#define CAMERASYSTEM_H
#include "Tickable.h"
class GameDriver;
class CameraSystem: public Tickable { //Complains here
//Stuff
};
#endif
Tickable.h:
#ifndef TICKABLE_H
#define TICKABLE_H
#include "GameDriver.h"
class GameDriver;
class Tickable {
//Stuff
};
#endif
Here is the error I get:
CameraSystem.h:9 error: expected class-name before '{' token
Forward declaring Tickable does not work either. Any help? Thanks in advance.
If you forward delcare a class you shouldn't include the class' header.
#include "CameraSystem.h" // DO NOT INCLUDE THIS
class CameraSystem; //Forward declaration
class GameDriver {
//Stuff
};
Ok, I see two problems here.
First: Your forward declarations are useless. You are writing (using GameDriver.h: as an example):
#include "CameraSystem.h"
class CameraSystem; //Forward declaration
class GameDriver {
//Stuff
};
Here CameraSystem will already be known by including CameraSystem.h, so the forward declaration is nonsense. What you meant to write was:
class CameraSystem; //Forward declaration
class GameDriver {
//Stuff
};
Second: Where you really would need a forward declaration you cannot use it, as you cannot derive from a forward declared class:
#include "Tickable.h"
class CameraSystem: public Tickable { //Complains here
//Stuff
};
Note that a forward declaration works only if you are not using the forward declared class directly as a class member. You are then restricted to pointers or references to the forward declared class. The reason for this is, that the compiler does not know the memory layout of the class unless its completely known (i.e. by including the header file). For example:
class CameraSystem;
class GameDriver {
CameraSystem m_cameraSystem;
};
will NOT work. However,
class CameraSystem;
class GameDriver {
CameraSystem *m_cameraSystem;
CameraSystem& m_otherCameraSystem;
};
will. Of course you will still have to include the correct header files in your *.cpp file.
You can't resolve such circular dependencies. However, you've got the solution (partially):
I.e. instead of including a header, just use a forward declaration. If you do this right, there shouldn't be any issues. However, try to group your program into logical elements/groups and only add cross-references if they're requied (e.g. the game has to know about and access the camera, but the camera shouldn't have to access the game system as a whole).
You can't inherit from a class that has not been declared.
Pre-processing Tickable.h gives you:
class GameDriver;
class CameraSystem: public Tickable { //Complains here
//Stuff
};
class CameraSystem; //Forward declaration
class GameDriver {
//Stuff
};
class GameDriver;
class Tickable {
//Stuff
};
See how Tickable hasn't even been forward declared when you inherit from it?
Try adding #pragma once at the very beginning of each header file.
You should somehow tell the compiler to only put those once. That's one way. Another is using #ifndef X and #define X.
If this doesn't solve it, try to just eliminate the circular dependency - it doesn't seem like it has to happen...

Two classes and inline functions

I have two classes and both of them uses some of the other class, on example:
// class1.h
class Class1;
#include "class2.h"
class Class1 {
public:
static Class2 *C2;
...
};
// class2.h
class Class2;
#include "class1.h"
class Class2 {
public:
static Class1 *C1;
...
};
And when I define it like in example above, it works (I also have some #ifndef to avoid infinite header recurency). But I also want to add some inline functions to my classes. And I read here that I should put definition of inline function in header file, because it won't work if I'll put them in cpp file and want to call them from other cpp file (when I do it I get undefined reference during linking). But the problem here is with something like this:
// class1.h
...
inline void Class1::Foo() {
C2->Bar();
}
I get error: invalid use of incomplete type ‘struct Class2’.
So how can I do it?
You need to delay including the header, but then include it and define your inline methods. By doing this in each header, they are self-sufficient and including one will always include the other, with include guards preventing infinite recursion.
A.hpp
#ifndef INCLUDE_GUARD_B9392DB18D114C1B8DFFF9B6052DBDBD
#define INCLUDE_GUARD_B9392DB18D114C1B8DFFF9B6052DBDBD
struct B;
struct A {
B* p;
void foo();
};
#include "B.hpp"
inline
void A::foo() {
if (p) p->bar();
}
#endif
B.hpp
#ifndef INCLUDE_GUARD_C81A5FEA876A4C6B953D1EB7A88A27C8
#define INCLUDE_GUARD_C81A5FEA876A4C6B953D1EB7A88A27C8
struct A;
struct B {
A* p;
void bar();
};
#include "A.hpp"
inline
void B::bar() {
if (p) p->foo();
}
#endif
You have it mix'd up. What you want is:
// class1.h
class Class2;
class Class1 {
public:
static Class2 *C2;
...
};
// class2.h
class Class1;
class Class2 {
public:
static Class1 *C1;
...
};
And include the respective headers in the source. The line:
class Class1; // or Class2
Declares an incomplete type, and you can have pointers and references to incomplete types. Upon usage, though, it needs to be complete. So just say "hey it'll exist!" in the header, and in the source tell it what it is.
My suggestion is that you place common methods and members into a base class, then derive C1 and C2 from the base class. This may fix the circular dependency issue.

C++ template syntax

I don't understand what is wrong with this code.
gcc reports "Client.h:29: error: expected template-name before '<' token"
As far as I'm aware I'm followed the template syntax correctly, but it could be that the error message is confusing me and is not the problem
client.h
class Client : public BaseDll<DllClient> [line 29]
{
..snip..
};
basedll.h
template<typename T>
class BaseDll : public Base
{
public:
..snip..
private:
T* _dll;
};
class Base{
};
template<typename T>
class BaseDll:public Base{
public:
private:
T* _dll;
};
class DllClient{
};
class Clien:public BaseDll<DllClient>
{
};
this compiled for me without problems, so I don't think the problem lies within what you posted. My best bet would be that you made a syntax error in client.h, maybe something as simple as forgetting a semicolon after another class definition or some macro that's messing with your code
I'm so sorry everyone, a school-boy error has been made, BaseDll is declared in another namespace. As soon as I added the namespace qualifier, the problem has gone.
Maybe it's just an easy problem: Have you included basedll.h in client.h?
to cover the basics:
does client-h #include basedll.h? do they user different include guards?
Further troubleshooting:
does it work with a non-template base class?
does it work then you typedef the template instaltiation:
typedef BaseDll<DllClient> tClientBase;
class Client : public tClientBase { ... }
[edit] OK, next:
if you put the following two lines directly under the BaseDll declaration:
template <typename T>
class BaseDll
{ ...
};
class DummyFoo;
typedef BaseDll<DummyFoo> tDummyFoo;
I think you snipped away the problem. Are you including something to define 'DllClient' ?
A possible cause of this is that there is an inter-dependency between the different header files:
// client.h
#ifndef CLIENT
#define CLIENT
#include "base.h"
// ...
class Client : public BaseDll<DllClient>
{
// ..snip..
};
#endif
// base.h
#ifndef BASE
#define BASE
#include "client.h"
template<typename T>
class BaseDll : public Base
{
public:
// ..snip..
private:
T* _dll;
};
#endif
Now imagine we're parsing 'base.cpp' then the preprocessor will do the following:
#include "base.h"
#ifndef BASE <--- BASE unset, keep going
#define BASE
#include "client.h"
#ifndef CLIENT
#define CLIENT
#include "base.h"
#ifndef BASE <--- BASE set, skip base.h, return to client.h
class client
: public BaseDll<DllClient> <-- ERROR, BaseDll not defined.
If this is the problem, then you potentially can get around it by forward declaring the base template in client.h:
// client.h
#ifndef CLIENT
#define CLIENT
// #include "base.h" <-- remove include
template <typename DLL_CLIENT>
class BaseDll;
// ...
class Client : public BaseDll<DllClient>
{
// ..snip..
};
#endif