Circular dependency expected class-name before '{' token - c++

sorry in advance to start another one of those circular dependency threads but I tried almost everything and maybe a fresh pair of eyes can help.
How do I get this * compiled?
CARD.H
#ifndef CARD_H
#define CARD_H
#include <string>
#include <sstream>
#include <irrKlang.h>
#include "Database.h"
using namespace std;
using namespace irrklang;
class Card: public Database{ // problem expected class-name before '{' token
public:
DATABASE.H
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <string>
#include <vector>
#include <sqlite3.h>
#include <wx/string.h>
#include <irrKlang.h>
#include <wx/file.h>
#include "Card.h" // even though i include card.h
using namespace std;
using namespace irrklang;
class Card; // if i take this out, I get: 'Card' was not declared in this scope|
class Database
{
public:
vector<Card> queryC(wstring query);

Two rules to help prevent circular dependencies:
1.) If you don't need the implementation of a class, declare it by forward reference only.
2.) If you need the implementation, include the header file as late as possible.
Card.h
#ifndef CARD_H
#define CARD_H
#include "Database.h"
class Card : public Database {
public:
int card;
};
#endif // #ifndef CARD_H
Database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <vector>
#include <string>
class Card;
class Database {
public:
std::vector<Card> queryC(std::string query);
};
#endif // #ifndef DATABASE_H
Card.cpp
#include "Card.h"
Card card;
Database.cpp
#include "Database.h"
Database database;
.
$ g++ -c Card.cpp -o Card.o
$ g++ -c Database.cpp -o Database.o
$ ls -l Card.o Database.o
-rw-r--r-- 1 user group 959 19. Sep 09:13 Card.o
-rw-r--r-- 1 user group 967 19. Sep 09:13 Database.o

Related

Include file includes itself with guards

I have two header files, animation.h and hero.h,
here is the code for animation.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
#ifndef ANIMATION
#define ANIMATION
//Class
#endif
And for hero.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
#ifndef HERO
#define HERO
//Class
#endif
I get the error message #include file "" includes itself even when using include guards.
I am new to using include guards and such so this may be a very trivial mistake.
Thanks for any help
You should put the header guards before anything else.
On gcc and MSCyou can also use
#pragma once
instead. Probably also on other more modern compilers. Simpliy put this at the top of your include, instead of the #ifdef....
animation.h
#ifndef ANIMATION
#define ANIMATION
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class
#endif
hero.h
#ifndef HERO
#define HERO
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
//Class
#endif
Or animation.h
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "hero.h"
//Class

C++ multiple definition errors in 3-way header include

I have 3 header files defining objects:
Point3d.h
#ifndef POINT3D_H
#define POINT3D_H
class Ray3d;
class Vector3d;
#include "Ray3d.h"
#include "Vector3d.h"
class Point3d {
...
};
#endif
Vector3d.h
#ifndef VECTOR3D_H
#define VECTOR3D_H
class Point3d;
class Ray3d;
#include "Ray3d.h"
#include "Point3d.h"
class Vector3d {
...
};
#endif
and Ray3d.h
#ifndef RAY3D_H
#define RAY3D_H
class Point3d;
class Vector3d;
#include "Point3d.h"
#include "Vector3d.h"
class Ray3d {
...
};
#endif
I won't include the .cpp files, but all the functions are defined there.
And then I have this class:
Transform.h
#ifndef TRANSFORM_H
#define TRANSFORM_H
#include <Eigen/Dense>
#include "../../geometry/Ray3d.cpp"
#include "../../geometry/Point3d.cpp"
#include "../../geometry/Vector3d.cpp"
using Eigen::MatrixXd;
class Transform {
...
};
#endif
AND FINALLY I have this subclass:
Translation.h
#ifndef TRANSLATION_H
#define TRANSLATION_H
//#include <Eigen/Dense>
#include "Transform.h"
//#include "../../geometry/Point3d.cpp"
//#include "../../geometry/Vector3d.cpp"
//#include "../../geometry/Ray3d.cpp"
using Eigen::MatrixXd;
class Translation : public Transform {
...
};
#endif
The problem is when I try to compile Translation.cpp:
g++ Transform.cpp Translation.cpp
I get a multiple definition of function error for every method in Ray3d, Point3d, and Vector3d. What can I do do avoid this? Should I be including less? Is my g++ command wrong? Thanks!
I'm also aware that I'm doing both forward declaration and includes in the first 3 headers, but that was the only way I could get those to compile. Part of the problem maybe?
You should not include the cpp files in transform.h
"but that was the only way I could get those to compile. Part of the problem maybe?"
You compile and link .cpp files separately, instead of including them (i.e. being seen from the preprocessor).
Your compiler command line should look rather like
g++ ../../geometry/Ray3d.cpp
../../geometry/Point3d.cpp
../../geometry/Vector3d.cpp
Transform.cpp Translation.cpp
-o MyExecutable

Including header file in header file

I have this code in C++:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "gspace.h"
class myclass {
public:
void update(gspace **);
}
gspace is another class defined in gspace.h. The compiler however is telling me:
include/myclass.h error: ‘gspace’ has not been declared|
Is there anything wrong in what I'm doing?
EDIT:
#ifndef GSPACE_H
#define GSPACE_H
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
class gspace
{
public:
gspace();
Assuming you have even declared a class named "gspace" in gspace.h I could think of two possible errors.
1) class gspace would not be in global scope i.e could be in some namespace.
2) Please check if header gaurds used in gspace not "MYCLASS_H".

Class Name does not declare type C++

I am trying to compile my code, but I am getting an error with classes. One of the classes compiled fine(Example.h), but the other(Name.h) keeps giving me the class does not name type error. I think it has something to do with circular dependency, but how do I fix that without a forward deceleration?
Name.h
#ifndef _NAME
#define _NAME
#include <iostream>
#include <string>
using namespace std;
#include "Example.h"
class Name{
};
#endif
Example.h
#ifndef _EXAMPLE
#define _EXAMPLE
#include <iostream>
#include <string>
using namespace std;
#include "Name.h"
class Example{
};
#endif
I saw a post about using forward deceleration, but I need to access memebers from the Example class..
You have a circular dependency, where each header tries to include the other, which is impossible. The result is that one definition ends up before the other, and the name of the second is not available within the first.
Where possible, declare each class rather than including the entire header:
class Example; // not #include "Example.h"
You won't be able to do this if one class actually contains (or inherits from) another; but this will allow the name to be used in many declarations. Since it's impossible for both classes to contain the other, you will be able to do this (or maybe just remove the #include altogether) for at least one of them, which should break the circular dependency and fix the problem.
Also, don't use reserved names like _NAME, and don't pollute the global namespace with using namespace std;
see, here you are including #include "Example.h" in Name.h and #include "Name.h" in Example.h. suppose compiler compiles Name.h file first so _NAME is defined now, then it tries to compile Example.h here compiler wants to include Name.h but the content of Name.h will not be included in Example.h since _NAME is already defined, hence class Name is not defined inside Example.h.
you can explicitly do forward declaration of class Name; inside Example.h
Try this:
Name.h
#ifndef NAMEH
#define NAMEH
#include <iostream>
#include <string>
using namespace std;
//#include "Example.h"
class Example;
class Name{
};
#endif
Name.cpp
#include "Name.h"
#include "Example.h"
...
Example.h
#ifndef EXAMPLEH
#define EXAMPLEH
#include <iostream>
#include <string>
using namespace std;
//#include "Name.h"
class Name;
class Example{
};
#endif
Example.cpp
#include "Example.h"
#include "Name.h"
...

Undeclared Identifier vector of pointers to objects

Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".