Here's the whole code getting the errors:
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "DXManager.h"
namespace XEngine
{
class Engine
{
};
}
#endif
DXManager.h
#ifndef DX_MANAGER_H
#define DX_MANAGER_H
namespace XEngine
{
class Engine; // forward declaration
class DXManager
{
public:
void run(Engine *engine);
};
}
#endif
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
using namespace XEngine;
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
With these 30 lines of code, I'm getting 20 errors like:
'XEngine' : a namespace with this name does not exist
'XEngine' : a symbol with this name already exists and therefore this name cannot be used as a namespace name
syntax error : identifier 'Engine'
Clearly, I'm missing something important here. What am I doing wrong?
note: I am aware that circular dependency is a bad thing, but in my particular case I believe that it is relevant.
In DXManager.cpp you are not just using some names from namespace XEngine. You define the function in that namespace.
So must be:
DXManager.cpp
#include <iostream>
#include "Engine.h"
#include "DXManager.h"
namespace XEngine {
void DXManager::run(Engine *engine)
{
std::cout<<"DXManager::run"<<std::endl;
}
}
AFAIK some of the compilers (like MSVC) process using variant too.
But it is not correct because your syntax tries to define function ::DXManager::run - not ::XEngine::DXManager::run you intend to define.
In the forward-declaration of class Engine the namespace XEngine doesn't exist at this point.
A workaround would be moving the declaration inside the namespace block.
When Engine.h includes DXManager.h, the latter defines a class XEngine::Engine without declaring the namespace first.
Related
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".
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".
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
{
...
};
I've looked around for a while, tried quite a few different approaches to this issue, but still cannot seem to get past errors occurring from forward declaration in a program with 3 codependent classes.
Here's a abstracted view of my current code's structure, split into 6 files + 1 main program file:
File x.h:
#ifndef X_H
#define X_H
using namespace std;
class y;
class x
{
private:
y *m_oY;
public:
// constructors &c
};
#endif
File x.cpp:
#include "x.h"
#include "y.h"
// Fancy stuff...
File y.h:
#ifndef Y_H
#define Y_H
using namespace std;
class z;
class y
{
private:
z *m_oZ;
public:
// constructors &c
z *funcZ()
};
#endif
File y.cpp:
#include "y.h"
#include "z.h"
// Fancy stuff...
File z.h:
#ifndef Z_H
#define Z_H
using namespace std;
class z { ... };
#endif
File z.cpp:
#include "z.h"
// Fancy stuff...
File main.cpp:
#include "z.h"
#include "y.h"
#include "x.h"
#include <iostream>
using namespace std;
int main() { ... }
The first error I receive, trying to compile in VS with a clean, non-PCH, non-ATL project occurs in my implementation when trying to use Class z. The error tells me that it's using the definition of z from y.h, and I'm not sure how to remedy this without creating a circular include problem. Text of the error follows:
main.cpp(114) : error C2514: 'z' : class has no constructors
y.h(9) : see declaration of 'z'
Any clues as to what I'm doing wrong here?
This isn't a forward declaration problem. Main.c can see the full declaration of class z. It must be that z has no constructor, at least of the proper shape, or maybe it is private.
guys i am new to c++
i am trying to create an class these are my files
//main.cpp
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test c;
c.set_url("e");
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
class test {
public:
void testing(string url);
};
#endif /* TEST_H_ */
//test.cpp
#include <iostream>
#include<string>
using namespace std;
void crawl::testing (string url) {
cout<< "i am from test class";
}
i am getting error: ‘string’ has not been declared error
The problem is you need to use the fully qualified name of string since the std namespace is not imported
class test {
public:
void testing(std::string url);
};
Please avoid the temptation to fix this by using the std namespace within the testing.h file. This is generally speaking bad practice as it can change the way names are resolved. It's much safer, albeit a bit annoying, to qualify names in header files.
The error you are getting is from not using the namespace std in the header file ie. std::string and not having the using namespace std; before your inclusion of the header file (or in the header).
The command using namespace std; is saying assume a class may be in this namespace, but it only applies to all the uses after the command.
If you did this instead it would also work, though in general this is bad form.
#include <string>
using namespace std;
#include "testing/test.h"
Also, don't forget to include test.h into test.cpp.