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".
Related
I'm aware this question has been asked numerous times but I've tried several suggestions such as checking my spelling, making sure I included the header files, capitalization, etc, but I'm still getting the same error and can't figure out what's triggering it.
When I try to compile Student.h using g++ -c Customer.h I get the error 'Student' does not name a type on the line 'Student student;' for Login.h and I have no idea why. Can anyone try to pinpoint what's causing it? This variable is supposed to represent the student of this login id/account which is supposed to be a pointer to a Student object.
Likewise, when I try to compile Login.h, I get the error 'Login' has not been declared in Customer.h for bool addAcct(Login*) as well as the error 'Login' does not have a type for Login* logins[MAX_LOGINS].
Any help would be appreciated!
Student.h:
#ifndef STUDENT_H
#define STUDENT_H
#define MAX_LOGINS 4
#include <string>
#include "Login.h"
using namespace std;
class Student{
public:
Student(int = 0, string = "");
int getId();
bool addAcct(Login*);
void print();
private:
int id;
string name;
Login* logins[MAX_LOGINS];
int numberOfLogins;
};
#endif
Login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <string>
#include "Student.h"
using namespace std;
class Login{
public:
Login(int = 0, float = 0);
int getNumber();
void setStudent();
void print();
private:
int number;
Student student;
};
#endif
Issue here is the circular dependency (as pointed out in the comments), and the problem with that is the processor essentially handles #include statements as sequential text insertions.
For example, when the preprocessor encounters #include "student.h", it goes step by step like:
#ifndef STUDENT_H // <--- header guard not defined at this point, ok to proceed
#define STUDENT_H // <--- define header guard first thing in order to prevent recursive includes
#define MAX_LOGINS 4
#include <string>
#include "Login.h" ---> #ifndef LOGIN_H
#define LOGIN_H
#include <string>
#include "Student.h" ---> #ifndef STUDENT_H
// entire body of student.h is skipped over
// because STUDENT_H header guard is defined
using namespace std; <--- // so processing continues here
class Login{
// ...
Student student; // <--- error because class Student is not defined
};
The solution is to forward declare types which do not require a full definition, instead of #include'ing the respective header.
In this case, class Login has a member Student student; which requires class Student to be fully defined, so login.h must in fact #include "student.h".
However, class Student only carries the Login* logins[MAX_LOGINS]; array of pointers to Login, and a pointer does not require a full definition of the class, but just a forward declaration of the type. Therefore Student.h can be modified to forward declare class Login instead, which removes the circular header dependency and allows the code to compile.
// #include "Login.h" // <--- full header not required
class Login; // <--- forward declaration is sufficient
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"
...
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
{
...
};
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.