I'm having an application with 2 winforms: Form1.h and TrackEdit.h. They're both in the same namespace ("ParkCleanUp2").
From within Form1 I call this code:
ParkCleanUp2::TrackEdit^ te;
Where it gives me these errors:
Error 24 error C2039: 'TrackEdit' : is not a member of 'ParkCleanUp2' (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Error 25 error C2065: 'TrackEdit' : undeclared identifier (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Error 26 error C2065: 'te' : undeclared identifier (TrackEdit.cpp) c:\users\-joey\documents\visual studio 2010\projects\park cleanup 2\park cleanup 2\Form1.h 2332
Though, if I go to TrackEdit.h it show me:
namespace ParkCleanUp2 {
//Some namespae includes
public ref class TrackEdit : public System::Windows::Forms::Form
So I'm wondering why it's giving me the error "'TrackEdit' : is not a member of 'ParkCleanUp2'" and why it's looking into the TrackEdit.cpp file, while I included the .h file.
What I found to be weird, and maybe important to mention, is that when I comment the #include "Form1.h line in TrackEdit.h it just works perfect, but in TrackEdit.h I than can't call Form1's functions (like selected an item in a listbox) which I wanted to achieve.
It appears you have both Form1.h and TrackEdit.h each #include-ing the other. Instead, have a forward declaration, and only include Form1.h from TrackEdit.cpp, and vise-versa.
The double-include doesn't work because you've got both classes referencing the other. Each class needs to know about the other in order to define itself. Since all you have is the full class definition, you've got a circular definition. Instead, the forward declaration provides just enough for the compiler to know "OK, there's a class with that name, and that's all I know about it", and the circular dependency is resolved.
(Also: When you edited the question, you removed the most important sentence: "so basically Form1.h includes TrackEdit.h, which includes Form1.h again". That pattern is very rarely correct. If you see yourself doing that, provide more forward declarations instead.)
Something like this:
Form1.h:
namespace ParkCleanUp2 {
ref class TrackEdit;
public ref class Form1 {
TrackEdit^ track;
};
}
TrackEdit.h:
namespace ParkCleanUp2 {
ref class Form1;
public ref class TrackEdit {
Form1^ parentForm;
};
}
Form1.cpp and TrackEdit.cpp:
#include "Form1.h"
#include "TrackEdit.h"
Related
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'm working on a project in c++ and I stuck with no idea what is wrong. I've writen 4 classes and everything looked fine during work (under visual studio 2010). VS 'saw' all the definition, i could use auto-fill and sudgestions, but when I tried to compile the project it sudennly went blind. It's like I didnt include headers or something (which I did). The strange thing is there is no problem with working with those classes on VS (i can ctrl+space for hint, list of attributes and methods and all that stuff) but when i try to compile it says "ClassName" is not a type.
Quick sample of problem below:
ProButton.cpp:
#include "ProButton.h"
using namespace pGUI;
ProButton::ProButton( ... )
: ProControl( ... )
{
...
}
ProButton.h:
#ifndef __PRO_BUTTON__
#define __PRO_BUTTON__
#include <string>
#include "ProControl.h"
namespace pGUI
{
class ProButton :
public pGUI::ProControl
{
public:
//attributes
...
public:
//methods
...
};
}
#endif
but compiler says:
Error 291 error C2653: 'ProButton' : is not a class or namespace name
for this line in ProButton.cpp: ProButton::ProButton( ... )
It also says:
Error 23 error C2039: 'ProControl' : is not a member of 'pGUI'
Error 24 error C2504: 'ProControl' : base class undefined
and all similar errors for whole project. I have no idea what is wrong. Looks like my VS broke :D
Of course those (...) means there is code there, just not that important for now. I can upload all solution somewhere fi it will help.
edit//
About namespaces, all header files (classes declaration) are defined in namespace with:
namespace pGUI{
class ProClass
{
};
}
all definitions for these classes (in ProClass.cpp) are using:
using namespace pGUI;
at the beginning.
I think the problem is with order of including files.
Im not sure how this is supposed to be done. So far i have 4 classes that:
class ProGUI:
has a pointer to ProContainer
includes: ProContainer and ProControl
class ProContainer:
has pointers to: ProGUI and ProControl
class ProControl:
has a pointer to ProContainer
includes ProButton
is a base class for ProButton
class ProButton:
is a sub-class of ProControl
Those classes also uses irrlicht library and I'm not sure where to include it.
I had it included in my main file just before #include "ProGUI.h". This is also the only include in main. ProGUI.h .
//EDIT 2 -> solved
It was a problem with includes. I needed to rethink my inclusion order and add some forward declarations. Anyway that all seemed strange and took me a lot of time to figure i out. Thx for help. :)
It seems that you are using following statement:
using namespace pGUI;
Just before the class declaration:
class ProControl
{
};
Instead of using following approach:
namespace pGUI
{
class ProControl
{
};
}
The using namespace, as it says uses a namespace. You need to explicitly put something a namespace using namespace keyword followed by braces!
using namespace pGUI informs the compiler that it should look in the pGUI namespace to resolve existing names.
To declare or implement something in a namespace you need to be more specific. with either:
namespace pGUI
{
ProButton::ProButton( ... ) : ProControl( ... )
{
...
}
}
or:
pGUI::ProButton::ProButton( ... ) : ProControl( ... )
{
....
}
Personally, I consider any use of using namespace to be a lazy programmer hack that completely defeats the point of namespaces. But I digress. :)
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.
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't seeing the rdbuf() procedure. Here is my code:
16. #include "DebugBuffer/BufferedStringBuf.h"
17.
18. BufferedStringBuf debug_buffer(256);
19. std::cout.rdbuf(&debug_buffer);
The BufferedStringBuf class is from this page: http://www.devmaster.net/forums/showthread.php?t=7037
Which produces the following error:
...src\main.cpp(19) : error C2143: syntax error : missing ';' before '.'
All I want to do is redirect std::cout to the Visual Studio Output window using OutputDebugString()..
You're not allowed to have executable statements at file-level scope. You can declare variables, but you can't call functions as standalone statements. Move your code into a function (such as gf's answer demonstrates), and you should have no problem.
Using the example class given on that site i don't have any problem:
#include <iostream>
#include "DebugBuffer/BufferedStringBuf.h"
class DbgBuf : public BufferedStringBuf {
public:
DbgBuf() : BufferedStringBuf(255) {}
virtual void writeString(const std::string &str) {}
};
int main()
{
DbgBuf debug_buffer;
std::cout.rdbuf(&debug_buffer);
}
Note that you have to create an instance of a class that derives from BufferedStringBuf because BufferedStringBuf::writeString() is pure virtual, thus making it an abstract class - abstract classes can't be instantiated.
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.