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...).
Related
I have been recently practicing managing multiple objects and drawing them in C++ using SFML library. I wanted my textures and future resources to be more reusable so I decided to make use of Thor library which suits my needs really well.
So I've written first few lines of code based on what you can find in this tutorial and the compiler always says:
main.cpp|12|error: 'textures_holder' does not name a type
This line gives an error :
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
I'm using Code::Blocks IDE with MinGW compiler and SFML 2.5.0.
Here's my main.cpp and the header file which contains extern object :
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//Rest of code...
Dirt_Block.h (only the upper part) :
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
I'd like to know what is causing this error and maybe help others who may experience similiar frustrating problems. Thanks for help.
EDIT :
As suggested in the comment I've declared a few extern int variables in the Dirt_Block.h so now it looks like this :
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
And then assinged to them some value in main.cpp :
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//...
But the compiler gives error :
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
Much less distracting to see what your problem is without all the extraneous code!
C++ programs don't start from the top of the file and run code down to the bottom. They start at the main(), and control flow proceeds from there, with one thing triggering another.
(Note: That doesn't take into account global constructor ordering, which does go in order of declaration--but you have no guarantee of the order declarations from "different files" might run in.)
Point being, you can't just make random function or method calls in the middle of a file. That's where you put declarations. You have to be inside of a function or method to make calls, e.g.
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png")
);
...
}
I am making a C++ program using Microsoft Visual Studio 2010.
When I add comments in the header file I get error messages on part of my code that is not included in the comment. If I remove the first two lines of comments under public, the errors under GameBoard(int p, int t) go away. If I remover all four comments under public: then the error under private: goes away.
Here are the error messages that I get:
Under GameBoard(int p, int t) I get an error under int that says Error: Expected an identifier. I also get an error under the comma that says Error: Expected a ')' And the last error I get is under the closing parenthesis that says Error: expected a ';'. But these three errors go away as soon as I remove the first two commented lines of the code shown below.
The other error I get is under private:, that says, Error: expected a declaration. This error goes away as soon as I remove all four commented lines under public:
Does anyone know why this is happening, commented lines aren't supposed to have any effect on the code. I've tried using the other kind of comment /* */ but the same thing happens.
using namespace std;
#include <string>
#include "Tile.h"
#include <iostream>
class GameBoard{
public:
// p = The number of players
// t = The number of tiles each player will play with.
GameBoard(int p, int t);
void init();
// Scan the board and see if any tiles would be exiled.
void checkExile();
// Clear the board of any tiles.
void clear();
private:
// The Game Board.
TileO board[12][12];
// Detained tiles. The height is determined by the number of players. Each player can detain up to three tiles.
TileO * detained[3];
// These are the tiles that are not in the game.
// All tiles start on the bench, where the player places them on the Home Base.
TileO bench[4][2][4];
int players; // Number of players
int tilesPlaying; // Number of tiles
};
Same thing happened to me once, I was trying to copy/paste codes from devC++ to VS2008. Maybe pasting somthing couses the problem.
Perhaps you have a bogus character in your source somewhere, like a UTF-8 BOM in the middle of it or something.
Try copying your code back out of Stack Overflow and pasting it over the top of your original - does that make a difference? (Or even just manually re-typing it - it's only a few lines.)
When I compile the program I am working on I get:
expected initializer before 'class'
error in my Class.h file. I looked up the error message on the internet, but couldn't find the exact error, although similar errors seem to be caused by missing semicolons but I don't see why I need one. This is the code the error points to, I have no other functions or classes before it.
class Account
{
public:
double dAccountBalance;
double dAccountChange(double dChange);
};
In the Class.cpp file the double dAccountChange(double dChange) function is defined. I don't think this is where the error is coming from but this is the code;
double Account::dAccountChange(double dChange)
{
dAccountBalance += dChange;
return 0.0;
}
When I change the code in Class.h to look like this,
;
class Account
{
public:
double dAccountBalance;
double dAccountChange(double dChange);
};
it doesn't generate an error message, but I can't work out why I need the semicolon before it as the only code I have before it are the following pre-processor lines.
#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED
Any ideas on why the error is generated?
Most likely, in the header file you include immediately before class.h, you'll have something like:
class xyzzy {
int plugh;
}
without the closing semi-colon. That will make your code sequence:
class xyzzy {
int plugh;
}
class Account
{
public:
double dAccountBalance;
double dAccountChange(double dChange);
};
which is clearly invalid. Inserting a semi-colon in class.h before the first line will fix it, but it's clearly the wrong place to put it (since it means every header file you include immediately after that one would need a starting semicolon - also, it's part of the definition in the first header and should be there).
Now that may not be the exact code sequence but it will be something very similar, and the underlying reason will be a missing piece of text in the previous header.
You should go back and put it in the earlier include file.
For example, consider:
include1.h:
class xyzzy {
int plugh;
}
include2.h:
class twisty {
int little_passages;
};
main.cpp:
#include "include1.h"
#include "include2.h"
int main (void) {
return 0;
}
Compiling this produces:
include2.h:3: error: multiple types in one declaration
but placing the semicolon at the end of include1.h (or start of include2.h though we've already established that's not a good idea) will fix it.
The problem is in one of the other headers, one that you #include ahead of class.h.
If you show us the top of your main cpp file, it might give a clue.
Compiling with Visual Studio 2005, on Windows XP. I add the following headers to my "stdafx.h" file like so:
#include <atlbase.h>
#include <atlcom.h>
#include <atlcore.h>
#include <atlstr.h>
(technically the same error appears with just atlbase.h included) which produces the following errors:
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2062: type 'double' unexpected
in the following code:
struct CheckValue : public unary_function<pair<MetID,double>,void>{
CheckValue(double _expected) : m_Expected(_expected){}
inline void operator()(const pair<MetID,double> &_val){
m_CheckList.push_back( near( _val.second ) ? 0 : 1 );
}
inline bool near(double _val){ //here is location of both errors
return ( m_Expected - m_Epsilon < _val ) || ( _val < m_Expected + m_Epsilon );
}
const static double m_Epsilon;
const double m_Expected;
list<int> m_CheckList;
};
const double CheckValue::m_Epsilon = 0.00001;
Without those lines added, no problems. Anyone want to venture a guess as to why? I'm scratching my head here and can't continue writing unit tests without those include files.
Run it through the preprocessor and see what you get. Maybe near is defined to something, or somesuch problem. (Hard to say without line numbers)
(I believe /E or /EP is the correct switches, but you can find it in the VS GUI options for a single file too..)
near is a macro defined in WinDef.h. When you include ATL headers, they probably indirectly include WinDef.h. Hence the error.
If you really need those headers, either stop using identifier near, or #undef it right after all headers are included.
The order of includes can sometimes cause weird things to happen, and indeed, such "known bugs" have happened in the past with VC++. Try shuffling the includes around to see if it helps.
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.