C++ undeclared identifier - c++

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.

Related

How to separate functions between .h and .c files

I am attempting to separate my function prototypes and the actual implementantions between .h and .c files to make it easier to see what functions I have.
my compiler, visual studio, has removed the curly green underline from under the function prototypes in the header files implying it is seeing the implementations in the .c file, but when I try to compile it I get 100+ errors all saying stuff like "syntax error: missing ';' before '*'" or "syntax error: missing ')' before '*'" or "syntax error: missing '}' before '*'" both in the .h and .c files. They are mostly pointing to the parameters in the prototypes, but I could not go trough all to know for sure.
Is there some other special way to do this?
I tried:
Making them extern
Changing guards to a name i know for sure isnt already used
I do not know what else I can try.
The first thing you could try is posting your code so we can see it :-)
However, the error syntax error: missing ';' before ... almost always shows up because you have an unknown type, as per the following:
int main() {
NOTYPE *x;
return 0;
}
Since you're moving function prototypes into a header, it may be that they're using types that still exist in the C file. Those type declarations must exist before use, or you will get that error.
That (function prototypes with unknown types) is not the only reason that error occurs but I can almost guarantee it'll be a similar issue, trying the use a type not yet declared.
As per other information posted here, you can also get this problem with circular dependencies, such as with:
typedef struct { TWO *two; } ONE; // don't know anything about TWO here.
typedef struct { ONE *one; } TWO;
If the dependency was only one way, then you could just ensure the order of the two definitions was correct. With a circular dependency, you have to introduce an incomplete definition (really a declaration) to allow it to work:
typedef struct TWO; // declare that TWO exists.
typedef struct { TWO *two; } ONE; // define ONE, we know TWO exists.
typedef struct { ONE *one; } TWO; // define ONE, we know all about ONE.
Note that this only works if you declare a pointer to the incomplete type, you won't be able to do:
typedef struct TWO;
typedef struct { TWO two; } ONE;
because the definition of ONE requires a complete type.
I have figured out the error.
The problem was that I had a circular dependency with 2 structures and I tried to fix it by defining them both before implementing them, but that obviously did not work.
this was my code:
typedef struct A;
typedef struct B;
typedef struct {
B* blist;
}A;
typedef struct {
A* a_parent;
}B;
The following stackoverflow answer helped me fix my errors:
Resolve circular typedef dependency?

Having linking classes issue? c++ I have no clue what is up

I have three classes.
first class:
#ifndef C_LINKED_LIST_H
#define C_LINKED_LIST_H
class CLinkedList {
private:
//removed code for brevity
public:
// removed code for brevity
};
#endif
second class:
#ifndef C_SSF_FOLDER_CONTAINER_H
#define C_SSF_FOLDER_CONTAINER_H
#include "C_SSF_Folder.h"
#include "CLinkedList.h"
class C_SSF_Folder_Container {
private:
// removed code for brevity
public:
int Add_Folder(C_SSF_Folder *_pcl_SSF_Folder);
C_SSF_Folder *Get_Folder(int _i_Index);
C_SSF_Folder *Get_Folder(char *_pch_Name);
//^-----errors
};
#endif C_SSF_FOLDER_CONTAINER_H
my third class
#ifndef C_SSF_FOLDER_H
#define C_SSF_FOLDER_H
#include <windows.h>
#include <fstream>
#include "C_SSF_Folder_Container.h"
using namespace std;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
my third class C_SSF_Folder.
I am including "C_SSF_Folder_Container.h"
and declaring a C_SSF_Folder_Container container.
Before declaring the variable it compiles fine. After I declare it
I get syntax errors in my C_SSF_Folder_Container
Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem\projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 16
Error C2061 syntax error: identifier 'C_SSF_Folder' CSSFileSystem \projects\cssfilesystem\cssfilesystem\c_ssf_folder_container.h 19
As I myself look into it I think there is a problem because my C_SSF_Folder is including C_SSF_Folder_Container.
and C_SSF_Folder_Container is including C_SSF_Folder
but the defines should take care of it? Other than that I have no clue what's the problem.
Everything is typed correctly.
You've got a circular #include -- C_SSF_Folder_Container.h #includes C_SSF_Folder.h and C_SSF_Folder.h #includes C_SSF_Folder_Container.h.
This would cause an infinite regress (and a compiler crash) except that you've got the #ifndef/#define guards at the top of your files (as you should); and because of them, instead what you get is that one of those two .h files can't see the other one, and that's why you get those errors.
The only way to fix the problem is to break the circle by deleting one of the two #includes that comprise it. I suggest deleting the #include "C_SSF_Folder.h" from C_SSF_Folder_Container.h and using a forward declaration (e.g. class C_SSF_Folder; instead.
C_SSF_Folder.h and C_SSD_Folder_Container.h are including each other(Circular Dependency).
When the compiler compiles C_SSF_Folder_Container object, it needs to create a C_SSF_Folder object as its field, however, the compiler needs to know the size of C_SSF_Folder object, so it reaches C_SSF_Folder object and tries to construct it. Here is the problem, when the compiler is constructing C_SSF_Folder object, the object has a C_SSF_Folder_Container object as its field, which is a typical chicken and egg question, both files depends on each other in order to compile.
So the correct way to do it is to use a forward declaration to break the circular dependency(including each other).
In your C_SSF_Folder.h, make a forward declaration of C_SSF_Folder_Container.
#include <windows.h>
#include <fstream>
using namespace std;
class C_SSF_Folder_Container;
class C_SSF_Folder {
public:
private:
C_SSF_Folder_Container cl_SSFFC_Folder_Container;
public:
};
#endif
Finally, include C_SSF_Folder_Container.h in your C_SSF_Folder.cpp.
You can also learn more in the following links:
Circular Dependency (Wiki):
https://en.wikipedia.org/wiki/Circular_dependency
Forward Declaration by Scott Langham
What are forward declarations in C++?

Child form calling function on parent in .NET c++

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"

Class member attributes error

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.

Syntax error missing ; before *

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.