I have a header file like so:
#pragma once
#include "gamestate.h"
#include "ExitListener.h"
class InitialGameState : public GameState
{
public:
InitialGameState(Ogre::Camera *cam, Ogre::SceneManager *sceneMgr, OIS::Keyboard *keyboard, OIS::Mouse *mouse, Ogre::Root *root);
~InitialGameState(void);
virtual bool update(Ogre::Real time);
virtual void pause(void);
virtual void start(void);
void keyPressed(const OIS::KeyEvent &e);
void keyReleased(const OIS::KeyEvent &e);
//private:
ExitListener *mFrameListener;
};
The problem with this is that I get the following errors from VC 8:
InitialGameState.h(16) : error C2143: syntax error : missing ';' before '*'
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(they all refer to the last line)
I have a class ExitListener.h which is why I don't get the errors
Edit: ExitListener.h:
#pragma once
#include <Ogre.h>
#include <OIS/OIS.h>
#include <CEGUI/CEGUI.h>
#include <OgreCEGUIRenderer.h>
#include "Thing.h"
#include "InitialGameState.h"
using namespace Ogre;
class ExitListener : public FrameListener, public OIS::KeyListener, public OIS::MouseListener
{
public:
ExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, Camera *cam, std::vector<Thing*> &vec):
mKeyboard(keyboard), r(0.09), mContinue(true), mRunningAnimation(false),
mMouse(mouse), mYaw(0), mPitch(0), things(vec), mCamera(cam), mWDown(false), mSDown(false), mADown(false),
mDDown(false)
{
things = vec;
mKeyboard->setEventCallback(this);
mMouse->setEventCallback(this);
}
bool frameStarted(const FrameEvent& evt);
bool keyPressed(const OIS::KeyEvent &e);
bool keyReleased(const OIS::KeyEvent &e);
bool mouseMoved(const OIS::MouseEvent &e);
bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);
void setOwner(GameState *g);
private:
AnimationState *mSwim;
Radian r;
Radian mYaw;
Radian mPitch;
OIS::Keyboard *mKeyboard;
OIS::Mouse *mMouse;
Camera *mCamera;
bool mContinue;
bool mRunningAnimation;
std::vector<Thing*> &things;
bool mWDown;
bool mADown;
bool mDDown;
bool mSDown;
GameState *mOwner;
};
Edit 2:
It turned out that the problem could be solved by a forward declaration and then including the other header directly in my .cpp file.
Thanks.
My guess is that ExitListener.h is including InitialGameState.h header file either directly or indirectly. So there is a circular dependency between the header file and compiler is unable to find the declaration for ExitListener. If you just need to store the pointer of ExitListener in this class then there is no need to include the ExitListener.h header file. Instead you can just use the forward declaration as class ExitListener;
EDIT: You can use the forward declaration as suggested above, or remove the InitialGameState.h include from ExitListener.h . You need to include GameState.h (the base class header file) only. But I prefer to use the forward declarations in header file and include the header file only in cpp.
The errors don't refer to the last line, but to the line before the last line. (Please be precise. If people know the compiler well which emits this error message, their guesses might be a lot better if they know the exact line it is given for.)
"ExitListener.h" is not a class, but a header. (This isn't Java.) One would assume that there is a class ExitListener defined (or at least declared) inside that header, but there could just as well be some other class, none at all, or many classes.
Without this header, it's impossible to say exactly what's wrong, although either circular dependencies between these two headers or a missing ; at the end of the ExitListener class' definition is a very good guess that fits my experience with such errors. At the very least I'm sure the error means that the compiler doesn't know what ExitListener is.
As others have said, you do not need a class definition in order to declare a pointer to that class, so (assuming that "ExitListener.h" defines the ExitListener class) you don't need to include the header at all. A simple forward declaration class ExitListener; is sufficient enough to declare the ExitListener *mFrameListener member. (You will need to include the full class definition in order to implement the InitialGameState member functions that deal with ExitListener, though. If you implement these functions in the header where InitialGameState is defined, you will need to keep that "ExitListener.h" include.)
Apparently the problem is with ExitListener definition, it's not considered valid at that point.
The error is in the ExitListener.h file (or any of the file it includes). Often this problem is due to a missing ; at the end of the class.
If you add the code of this file I will be able to help you further.
The problem
ExitListener was incorrectly declared. This is the only solution for VS to say this.
Check that there was no error when compiling the ExitListener class. (and that you did not forget the trailing ";")
A side note
Here you are using a pointer to ExitListener. You do not need to know the size or internal layout of ExitListener if you just declare a pointer.
A forward declaration would be enough.
I suspect you are missing the Ogre includes somewhere on your include chain.
My assumption is based on the little knowledge I have about your other header files and VC alerting that it's missing a type specifier: missing type specifier.
Related
I'm trying to compile a project that has the following header:locale.h;
locale.h:
class LOG4CXX_EXPORT Locale
{
public:
...
protected:
Locale(const Locale&);
Locale& operator=(const Locale&);
const LogString language; <-- error
const LogString country; <-- error
const LogString variant; <-- error
}; // class Locale
Could anyone give me some suggestions ?
I'm getting this error. I am not sure
what is the problem.
/LOGGER/include/log4cxx/helpers/locale.h:42:41: error: field ‘language’ has incomplete type
const LogString language;
^
/LOGGER/include/log4cxx/helpers/locale.h:43:41: error: field ‘country’ has incomplete type
const LogString country;
^
/LOGGER/include/log4cxx/helpers/locale.h:44:41: error: field ‘variant’ has incomplete type
Consider the following code:
class MyClass;
int method1(const MyClass& param);
MyClass& method2();
const MyClass instance; // <- error here
The declaration of MyClass is a forward declaration. All the compiler know is that the class exists (it doesn't know its members, size...), that's why it is called an incomplete type. You can use references or pointers of that class, but that's it. See more info here When can I use a forward declaration?
So it seems that in your code, you only have a forward declaration of LogString type, and not a full declaration. Check your include files and include order so you get the full declaration of this class.
You are using std::basic_string, but there is no include for the appropriate header file:
#include <string>
What AnT wrote in a comment is the solution to the problem:
<clocale> is including your <locale.h> instead of the system one it ought to do; your locale is trying to include <string>, which again includes <clocale>.
So in the end, you get a circular include as I described in your other question https://stackoverflow.com/questions/32379927/header-file-does-not-compile-locale-h, just the chain being longer...
You need to break this inclusion circle. You can do this by removing the directory the file resides in from the inclusion directories you pass to gcc (I suppose this is -I"/LOGGER/include/log4cxx/helpers"). Instead, you can give a path to the parent directory (-I"/LOGGER/include/"). Instead of #include <locale.h> you would have to use #include <log4cxx/helpers/locale.h>.
Actually, I recommend keeping "/LOGGER/include" as the only directory you give gcc and have all other files you need included via the corresponding subpath - provided the rest of the log4cxx files allow that (which I would assume).
Apart from that, the only other way to solve the problem is indeed renaming your 'locale.h' file to something else (apart from changine the include path division, such as -I"/LOGGER/include/log4cxx" and #include <helpers/locale.h>; the one I chose, however, is the most natural one IMO).
I have a C++ project in VS with more or less 100 files (each file is a class). I modified one a couple of days ago, adding some declarations, and now I can't compile and it gives a lot of errors and this one lastly:
error count exceeds 100; stopping compilation
Posting the errors seems useless, but here are some (the are all pretty much the same):
error C2275: 'btTransform' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2504: 'Platform' : base class undefined
error C2535: 'btAlignedObjectArray<T>
Note than most of the mentioned errors shouldn't be errors, and IntelliSense shows no error in the error list output. And I am completely sure I forgot a ; or something similar.
What should I do? Also I am working with a lot of stuff, and I forgot which file I modified. I browsed through most of them and couldn't find anything.
Here is the complete list: http://pastebin.com/1CD9fGgn (it is so long that it doesn't fit here)
As requested:
Player.h
#pragma once
#include <Ogre.h>
#include "BtOgreGP.h"
#include "BtOgrePG.h"
#include <OISKeyboard.h>
#include <OISJoyStick.h>
#include "BulletCollision\CollisionDispatch\btGhostObject.h"
#include "balance.h"
#include "WorldGenerator.h"
#include "Keys.h"
#include "PlayerController.h"
using namespace Ogre;
class Player
{
public:
Player(Root*, SceneManager*, RenderWindow*);
~Player(void);
Camera* mCamera;
void update(const FrameEvent&);
bool isTouchingBelow();
// bool isJumping();
btPairCachingGhostObject* getGhostObject()
{
return mGhostObject;
}
void clearObjectTouchingNormal()
{
mNormals->clear();
}
void addObjectTouchingNormal(btVector3* vector)
{
mNormals->push_back(*vector);
}
btAlignedObjectArray<btVector3> getObjectTouchingNormal()
{
return *mNormals;
}
private:
btAlignedObjectArray<btVector3>* mNormals;
double mTimeLastJump;
WorldGenerator* mGenerator;
bool mPressJumpLastUpdate;
// btAlignedObjectArray<btVector3> getObjectTouchingNormal();
Vector3 mLastVectorVelocity;
//SceneNode* mCameraHelper;
SceneNode* mMainNode;
SceneNode* mBodyNode;
SceneNode* mCameraPivot;
SceneNode* mCameraYaw;
SceneNode* mCameraPitch;
SceneNode* mCameraHolder;
SceneManager* mSceneManager;
BtOgre::DebugDrawer* mDebugDrawer;
//btRigidBody* mPlayerBody;
btQuaternion* mDefaultQuaternion;
Vector3 mStartPosition;
PlayerController* mKinematicController;
btPairCachingGhostObject* mGhostObject;
//bool mIsJumping;
Radian mLastRotation;
btVector3 mBodyDimensions;
/*bool mCameraCenterMovementFlag;
Radian mCameraCenterYaw;*/
};
class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback
{
protected:
btRigidBody* mMe;
public:
ClosestNotMe (btRigidBody* me) : btCollisionWorld::ClosestRayResultCallback(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 0.0, 0.0))
{
mMe = me;
}
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace)
{
if (rayResult.m_collisionObject == mMe)
return 1.0;
return btCollisionWorld::ClosestRayResultCallback::addSingleResult (rayResult, normalInWorldSpace);
}
};
Globals.h
#pragma once
#include <Ogre.h>
#include <vector>
#include "Player.h"
enum GameDifficulty
{
GD_EASY,
GD_NORMAL,
GD_HARD
};
class GlobalVariables
{
public:
static std::vector<Player*> players;
};
Scroll to the top of your error list, and deal with that one. If you're in Visual Studio, you can compile it and hit Ctrl-Shift-F12.
If you make some error in syntax (my usual one is an unmatched quote or brace), the compiler can lose its context, so everything from that point forward becomes unintelligible to it. The only way you'll get through it is to correct the first error found; and then--if there are still errors--the next one. At some point, you'll find that thing you did, and the rest of the errors will magically disappear.
Welcome to the wonderful world of c++ errors.
When faced with something like this start at the first error reported (Look at the output from the compiler). Then recompile. Repeat until there are no more errors.
There are a lot of different syntax errors that completely muck up the rest of the file. These include bad type in a declaration, missing semi-colon, and missing braces.
I've seen over 200 errors disappear with a one character fix.
Also, if you forget a semi-colon or brace in one header file, it might cause errors in the next included header. I've had really bad errors in windows.h, because I've included my header before it and forgotten something.
Solving tricky compiler error problems like the one you describe become much easier when you dump the (annotated) preprocessed output and look at the information that the compiler is getting. cl.exe takes the /E argument in order to dump the preprocessed output to stdout (see also this SO answer). If you use that, and look at the error in that context, it should be clear.
An error like 'Player': undeclared identifier showing up in the file where you think you're defining that very identifier is likely caused by a circular dependency. For example, does PlayerController.h contain references to the Player class without a forward declaration?
Source control is key. Based on your comment and extra code, please also post player.h contents.
In general in such a horrible case I do binary search with commenting out large portions of the project.
An error in an h file included in several cpps is usually more likely to crate lots of errors from one little mistake. A cpp parsing issue only affects the cpp. Look for an error in an h file.
Also general word of advice, try to keep all the h file references in the cpp files and limit the references made to h files from h files to an absolute minimum, this will reduce include nightmares. As you can see player.h includes a large amount of h files. Each one of them could be the culprit, I suggest commenting them one by one and wait for an error about an undefined symbol instead of the errors you see now. This will mean that you no longer have a parsing error and now have a missing definition (thus the error is in the file you commented out or in one of the files it includes...).
I want to create a sea battle game. I have two classes: Ship and Cell.
#pragma once
#include"stdafx.h"
#include"Globals.h"
#include<vector>
#include"MCell.h"
class Ship
{
private:
int lenght;
int oriantation;
vector<Cell*> cells;
vector<Cell*> aroundCells;
...
#pragma once
#include<vector>
#include"MShip.h"
class Cell
{
private:
bool haveShip;
bool selected;
bool around;
int x;
int y;
Ship* ship;
And i have got many error like those:
1>projects\seewar\seewar\mship.h(13): error C2065: 'Cell' : undeclared identifier
1>projects\seewar\seewar\mship.h(13): error C2059: syntax error : '>'
1>projects\seewar\seewar\mship.h(14): error C2065: 'Cell' : undeclared identifier
What's wrong with the code?
Well your problem lies that when you include MCell.h you include MShip.h which references Cell defined in MCell.h. However MShip.h refers to MCell.h which won't get included because of the pragma once. If the pragma once wasn't there then you'd get an inifinite loop that would stack overflow your compiler ...
Instead you could use a forward declaration.
ie remove the #include "MCell.h" from MShip.h and replace it with, simply, "class Cell;" All your circular references issues will this go away :)
Your header files depend on each other. However one of them will have to be read before the other. So you need to rewrite one of them (or both of them) to not depend on the other. You can do that by forward-declaring the classes instead of including the header file that defines them.
So in MShip.h you should put a declaration class Cell; instead of including MCell.h and/or vice versa.
You need to forward declare the classes.
Ship.h
class Cell; //forward declaration
class Ship
{
//....
};
Cell.h
class Ship; //forward declaration
class Cell
{
//....
};
and remove the circular inclusion. You don't need to include one header in another since you're not working with complete types, but pointers. Full class definition is required when you use concrete objects of the type. In the case of pointers, you don't.
I have two classes and want to have a reference from class Kunde to class Konto and backward, but my compiler shows many errors. I don't know what the problem is. Please help me.
Class Konto:
#pragma once
#include "Kunde.h"
class Konto {
private:
Kunde* kunde;
protected:
int kontonummer;
double stand;
public:
int getKontonummer();
Kunde* getKunde();
double getKontostand();
bool einzahlen(double betrag);
virtual bool auszahlen(double betrag);
};
Class Kunde:
#pragma once
#include "Konto.h"
#include <string>
class Kunde {
private:
string vorname;
string nachname;
Konto* konto;
public:
Kunde(string vorname, string nachname);
void setKonto(Konto* konto);
Konto* getKonto();
};
I get following compiler errrors:
konto.h(6): error C2143: syntax error: missing ';' before '*'
konto.h(6): error C4430: missing typespecifier - int assumed. Note: C++ does not support "default-int"
konto.h(6): error C4430: missing typespecifier - int assumed. Note: C++ does not support "default-int"
and some more.
The header files can't include each other. Instead of the #includes, try a forward declaration in one or both, like this:
class Kunde;
You have a circular inclusion problem. You see the #pragma once statement in the first line of the header file? This prevents an inclusion of the header if it has already been included. Since your header files include each other, at the declaration of either Kunde or Konto the other one has not yet been defined.
You can circumvent the problem if you make a simple forward declaration of either class in the other header file. Specifically:
(Konto.h)
#pragma once
// Do NOT include Kunde.h
class Kunde;
class Konto {
// your further class definition as normal.
The only thing is that you now should include Kunde.h in the Konto.cpp, or else this would lead to a linker error.
EDIT: see comments :) thanks
Including one file in another that includes the first file, that includes the second file that includes the first file...
surely will confuse #pragma once
Konto is including Kunde.h and Kunde is including Konto.h. Do a forward declaration in both cases
This is a classic circular dependency. You can handle it a couple ways. The first is to use forward declarations for the other class you are trying to reference. You'll need to remove the include for the other class too.
class Konto;
class Kunde
{
Konto* konto;
...
};
The other way is to abstract out an interface that gives you what you want. I can go into further detail on that approach if you like.
I checked out a post similar to this but the linkage was different the issue was never resolved. The problem with mine is that for some reason the linker is expecting there to be a definition for the base class, but the base class is just a interface. Below is the error in it's entirety
c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxdx.h(2) : error C2504: 'GXRenderer' : base class undefined
Below is the code that shows how the headers link with one another
GXRenderManager.h
#ifndef GXRM
#define GXRM
#include <windows.h>
#include "GXRenderer.h"
#include "GXDX.h"
#include "GXGL.h"
enum GXDEVICE {
DIRECTX,
OPENGL
};
class GXRenderManager {
public:
static int Ignite(GXDEVICE);
private:
static GXRenderer *renderDevice;
};
#endif
at the top of GxRenderManager, there is GXRenderer , windows, GXDX, GXGL headers. I am assuming by including them all in this document. they all link to one another as if they were all in the same document. correct me if I am wrong cause that's how a view headers. Moving on...
GXRenderer.h
class GXRenderer {
public:
virtual void Render() = 0;
virtual void StartUp() = 0;
};
GXGL.h
class GXGL: public GXRenderer {
public:
void Render();
void StartUp();
};
GXDX.h
class GXDX: public GXRenderer {
public:
void Render();
void StartUp();
};
GXGL.cpp and GXDX.cpp respectively
#include "GXGL.h"
void GXGL::Render()
{
}
void GXGL::StartUp()
{
}
//...Next document
#include "GXDX.h"
void GXDX::Render()
{
}
void GXDX::StartUp()
{
}
Not sure whats going on. I think its how I am linking the documents, I am not sure.
The problem is You need to have #include "GXRenderer.h" at the top of both: GXGL.h and also GXDX.h.
The base type must be defined not just declared before defining a derived type.
By the way, the error is a compiling error not linking error.
Edit: About your class type redefinition:
at the top of every header file you should have #pragma once.
The #pragma once directive specifies that the file will be included at most once by the compiler in a build.
You included them all into GXRenderManager.h, meaning that GXRenderManager.h is OK.
But you forgot to include them all into GXGL.cpp and GXDX.cpp. In these .cpp files GXRenderer class is completely unknown.
There are at least two "schools" of #include strategies. One says that header file must include everything that is needed for its own compilation. That would mean that GXGL.h and GXDX.h must include GXRenderer.h. If you followed that strategy, your GXGL.cpp and GXDX.cpp would be OK as they are now.
Another "school" says that header files must not include each other at all, i.e. all inclusions must be done through .cpp files. At first sight one could guess that your GXGL.h and GXDX.h follow that strategy (since you are not including anything into them), but then your GXRenderManager.h looks completely different.
You need to decide which strategy you are trying to follow and follow it. I'd recommend the first one.
I got an error C2504: 'CView' : base class undefined
where CView is not directly my base class from which I am inheriting.
I am inherting mYClass from MScrollView, "for this matter any class which is not actual Base Class is what the point is to be noted down here"
but the error is the C2504. When I have included it in the header where this problem is arising, this problem is resolved.
#include "stdafx.h"
where stdafx.h has #include which contains all the basic class defined...hope this answer resolves everyone who are facing this issue.