I have a class SetStructure with this .h file:
#ifndef setstructure_h
#define setstructure_h
#include "Matrix.h"
class SetStructure {
public:
SetStructure();
//some other functions
};
#endif
And a training class with this .h file:
#ifndef training_h
#define training_h
#include "SetStructure.h"
#include "Matrix.h"
class Training {
private:
//some functions
public:
//some functions
};
#endif
When I compile everything, I get this error in my implementation file for Training:
SetStructure training = new SetStructure();
This gives an error
'SetStructure' was not declared in this scope.
Furthermore the compiler thinks all my functions in Training return int even though they're specified as other return types in the Training.h file.
Is there some problem with my #include statements or something else which might lead to this issue?
Related
This is my EngineIncludes.h file:
#include <stdafx.h>
//Engine Includes
namespace Engine
{
//Classes: 23
class BaseObject;
class Console;
class Engine;
class Node;
template <class T> class Transform;
}
#include "core/BaseObject.h"
#include "core/Console.h"
#include "core/Engine.h"
#include "math/Transform.h"
#include "scene/Node.h"
//Global Objects
extern Engine::Console* CONSOLE;
extern Engine::Engine* CORE_ENGINE;
In stdafx.h I have regular stuff like OpenGL, std::map, boost... and I'm building a precompiled header as standard.
This is the Node.h file:
#ifndef _NODE_H
#define _NODE_H
#include <stdafx.h>
#include <EngineIncludes.h>
namespace Engine
{
class Node : public BaseObject
{
public:
Node();
~Node();
void SetParent(Node* parent);
Node* GetParent();
void SetTransform(const Transform<double> &transform);
Transform<double> GetTransform();
Transform<double> GetDerivedTransform();
Transform<double> GetInheritedTransform();
void Update();
private:
Transform<float> transform;
Transform<float> derived;
Node* parent;
};
}
#endif _NODE_H
I get 3 errors here. One C2504 that Engine::BaseObject is not defined. And two C2079 that both Transform transform and Transform use undefined class. Now this is the BaseObject.h:
#ifndef _BASE_OBJECT_H
#define _BASE_OBJECT_H
#include "stdafx.h"
#include "EngineIncludes.h"
namespace Engine
{
class BaseObject
{
public:
BaseObject()
{
id = CORE_ENGINE->GenerateID();
}
~BaseObject()
{
}
long GetID()
{
return id;
}
private:
long id;
};
}
#endif _BASE_OBJECT_H
Now I specifically fully defined BaseObject inside header file with no luck. Still the same error. But if I comment out forward declarations in EngineIncludes.h, the compiler freaks out with
extern Engine::Console* CONSOLE;
It literally ignores all #includes for whatever reason. I've never had an issue like this before. And I tried everything. I even created EngineIncludes.cpp file. I moved the contents of EngineIncludes.h into stdafx.h with no luck. It somehow just ignores #includes. Even if I put #include "BaseObject.h" inside "Node.h" nothing changes. When it compiles Node it has both Transform which is a template class and BaseObject undefined, even though both objects are clearly defined before Node with forward declarations and #includes.
I'm using Visual Studio 2010, but never had an issue like this. I looked into my previous projects and all is written the same fashion and works without problem.
My previous question was asked wrong, so I'll post it fixed.
I have this example throwing
expected class-name before ‘{’ token
error while compiling. I am understanding why is it fails, but I don't know how to fix it. Thank you.
BaseClass.h
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h"
class ElementClass;
class BaseClass
{
private:
ElementClass *m_someField;
};
#endif
ElementClass.h
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h"
class ChildClass;
class ElementClass
{
private:
ChildClass *m_class;
};
#endif
ChildClass.h
#ifndef INHERITTEST_CHILDCLASS_H
#define INHERITTEST_CHILDCLASS_H
#include "BaseClass.h"
class ChildClass : public BaseClass
{
};
#endif
You have circulary dependent .h files.
In BaseClass.h:
#ifndef INHERITTEST_BASECLASS_H
#define INHERITTEST_BASECLASS_H
#include "ElementClass.h" // Includes ElementClass.h
In ElementClass.h:
#ifndef INHERITTEST_ELEMENTCLASS_H
#define INHERITTEST_ELEMENTCLASS_H
#include "ChildClass.h" // Which included BaseClass.h
You can remove those #include lines since you are using those classes by pointers only and a forward declaration is sufficient for that purpose.
When you're working with inheritance the following
#include "ChildClass.h"
class ChildClass;
is unnecessary, if you're going to break these into sepperate source files (which it looks like you are) you can say
#include "ElementClass.h"
in the source file of your derived class
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".
I'm trying to include files in my c++ program but I keep encountering the error:
ShapeVisitor.h:9:28: error: ‘Circle’ has not been declared
I think the problem is that the way the classes are structured, it results in a circular dependence. How do I solve this?
The class headers are below...
//Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
// headers, ...
#include "Shape.h"
class Circle: public Shape {
//class declaration
}
#endif
//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
// headers, ...
#include <iostream>
class Shape {
//a certain method in the class declaration looks like this
virtual void accept(ShapeVisitor& v) = 0;
//rest of class
}
#endif
//ShapeVisitor.h
#ifndef SHAPEVISITOR_H
#define SHAPEVISITOR_H
#include "Circle.h"
class ShapeVisitor {
//a method in the class looks like this:
virtual void visitCircle(Circle *s) = 0;
//rest of class
}
#endif
As you can see, circle includes shape, which includes shapevisitor, which again, includes circle.
Any ideas?
ShapeVisitor.h does not need to include Circle.h, a forward declaration class Circle; will do. Function declarations do not require the full definitions of their argument and return types (not even if the return/arguments are by value!). Only the function's implementation file (in your case: ShapeVisitor.cpp) would need to include Circle.h.
This ancient (but still very true!) column by Herb Sutter is a nice reference.
So I am getting the following errors:
..\Actor.h:35: error: `Attack' is not a member of `RadiantFlux'
..\Actor.h:35: error: template argument 1 is invalid
..\Actor.h:35: error: template argument 2 is invalid
..\Actor.h:35: error: ISO C++ forbids declaration of `attacks' with no type
On this line (among others):
std::vector<RadiantFlux::Attack> attacks;
Here are the relevant files:
Actor.h:
#ifndef ACTOR_H_
#define ACTOR_H_
#include <string>
#include <vector>
#include "Attack.h"
namespace RadiantFlux {
...
class Actor {
private:
std::string name;
int health;
std::vector<RadiantFlux::Attack> attacks;
Attributes attributes;
public:
...
};
}
#endif /* ACTOR_H_ */
Attack.h:
#ifndef ATTACK_H_
#define ATTACK_H_
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Actor.h"
namespace RadiantFlux {
...
class Attack {
private:
...
public:
...
};
}
#endif /* ATTACK_H_ */
Why am I getting these errors and what can I do to fix them? I am assuming it has something to do with the namespaces...
You have a cyclic dependency of your header files.
Attack.h includes Actor.h and vice versa.
Use Forward Declaration of class to avoid circular dependency problems.
Since the OP's comments, here is what needs to be done:
class Actor;
class Attack
{
};
If your code fails to compile after doing this, You need to read the linked answer and Understand why the error and how to solve it. The linked answer explains it all.
The classes Actor and Attack both refer to each other, so you will need to add a forward declaration in one of the file.
For example, in Actor.h:
class Attack;
class Actor
{
...
};