Syntax errors in C++ include file - c++

I'm writing a game in c++ in microsoft visual studio 2010, yesterday I wrote a pong game and everything was fine but now the compiler telling me that there is a lot of errors for example:
1>w:\c++\planet escape\planet escape\room.h(25): error C2061: syntax error : identifier 'WorldMap'
And here is the Room.h file:
#pragma once
#include <allegro5/allegro.h>
#include <vector>
#include "Entity.h"
#include "WorldMap.h"
#include "Link.h"
#define ROOM_W 20
#define ROOM_H 20
class Room{
private:...
public:...
};
When in code there is no mistakes and it sees all the classes fine.
So what can cause such mistake?
EDIT:
here is the WorldMap.h
#pragma once
#include <allegro5/allegro.h>
#include "Room.h"
#include "Player.h"
#define WORLD_W 10
#define WORLD_H 10
class WorldMap{
private:...
public:...
};
If when I'm runing it he cant see it then why he see it when coding?

You have circular includes. Suppose you are compiling a file that has a #include "WorldMap.h" as the first applicable #include statement. The file WorldMap.h has that #include "Room.h" that is going to cause a lot of trouble. The problems start in earnest in Room.h at the #include "WorldMap.h" statement. That #include has no effect thanks to the #pragma once in WorldMap.h. When the compiler gets to the point of processing the main body of Room.h, the class WorldMap is neither defined nor declared.
Addendum
The solution is to get rid of those extraneous #include statements. The file WorldMap.h does not need to #include either of Room.h or Player.h. It instead needs to make forward declarations of the classes Room and Player. Similarly, you don't need all those #include statements in Room.h either.
It is in general a good idea to use forward declarations of types in your headers instead of including the file that defines the types. If the code in the header does not need to know details of the type in question, just use a forward declaration. Do not #include the header that defines the type.

Related

How to properly include headers and use STL vector in c++ app?

I decided to try STL and use a vector instead of a custom made growable array class. The problem is that I can't get anything to compile. If I do something like this:
#include "stdafx.h"
#include <vector>
std::vector<PITEMID_CHILD> APIDL;
I get a bunch of messages similar to this:
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\cstdint(23): error C2039: 'int_least8_t': is not a member of '`global namespace''
If I change to this:
#include <vector>
#include "stdafx.h"
std::vector<PITEMID_CHILD> APIDL;
I get this:
1>x:\win32testing\vectortest\vectortest.cpp(4): error C2039: 'vector': is not a member of 'std'
Inside of stdafx.h is this:
#pragma once
#include <windows.h>
#include "targetver.h"
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <shlobj.h>
#include <exdisp.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <atlbase.h>
#include <atlalloc.h>
#include <CommonControls.h>
// reference additional headers your program requires here
#include <CommCtrl.h>
Any idea what is going on?
From wikipedia documentation:
Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The best solution is to get rid of precompiled headers.
And for your C2039 error, it doesn't seem to be a result of line std::vector<PITEMID_CHILD> APIDL;. int_least8_t is a type defined in cstdint (stdint.h). It seems you haven't included this header file in your project.

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)

I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.
#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Really!! How do you do it?";
_getch();
}
But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined.
Why is this happening?
Since Psapi.h's include tree is trivial, I'm going to exemplify. Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):
BOOL is defined in minwindef.h (#157: typedef int BOOL;)
Psapi.h only includes one file (#27: #include <winapifamily.h>)
winapifamily.h doesn't include any other file
So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.
Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.
Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that. Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)
#include <Windows.h>
#include <WinSock2.h>
// main present just for rigorosity's sake
int main() {
return 0;
}
to answer the question, I know this is DATED but the issues persist today. You need the following:
#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h>
#include <psapi.h>
After stdlib.h was included, the errors were gone.

Having difficulty resolving a circular dependency problem when including a windows header

I am new to using COM interfaces, and there's one method in particular that I would like to call in my cppWinRT app:
ICompositionGraphicsDeviceInterop::GetRenderingDevice()
The first problem I am having is what appears to be a circular dependency problem when I try to add #include <windows.ui.composition.interop.h> into my pch.h, which currently looks like this:
#pragma once
#include <windows.h>
#include <unknwn.h>
#include <restrictederrorinfo.h>
#include <hstring.h>
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.ApplicationModel.Activation.h"
#include "winrt/Windows.UI.Xaml.h"
#include "winrt/Windows.UI.Xaml.Controls.h"
#include "winrt/Windows.UI.Xaml.Controls.Primitives.h"
#include "winrt/Windows.UI.Xaml.Data.h"
#include "winrt/Windows.UI.Xaml.Markup.h"
#include "winrt/Windows.UI.Xaml.Navigation.h"
#include <winrt/Windows.UI.ViewManagement.h>
#include "winrt/Windows.ApplicationModel.Core.h"
#include "winrt/Windows.UI.Core.h"
#include <windows.ui.composition.interop.h>
I get the following compiler error:
c:\program files (x86)\windows kits\10\include\10.0.17744.0\winrt\windows.ui.composition.interop.h(100): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I tried to figure out which type is causing the problem, but I can't figure out which one at line 100 of windows.ui.composition.interop.h is problematic.
How can I solve this?
There was a macro at line 100, which required me to include "combaseapi.h".

how to swig omit forward declaration

I am trying to run swig on vendor header so I do not need to redefine the implementation in a separate header file
swig -go -cgo -intgosize 64 -module qt -o $WORK/qt/_obj/qt_wrap.cxx -outdir $WORK/qt/_obj/ \
-I$HOME/Qt5.9.1/5.9.1/gcc_64/include -I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtOpenGL \
-I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtWidgets -I$HOME/Qt5.9.1/5.9.1/gcc_64 \
/include/QtGui -I$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtCore -c++ qt.swigcxx
I am getting the error below during go build
$HOME/Qt5.9.1/5.9.1/gcc_64/include/QtWidgets/qapplication.h:57: Error:
Syntax error in input(1).
Upon inspecting
qapplication.h:57
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qcoreapplication.h>
#include <QtGui/qwindowdefs.h>
#include <QtCore/qpoint.h>
#include <QtCore/qsize.h>
#include <QtGui/qcursor.h>
#ifdef QT_INCLUDE_COMPAT
# include <QtWidgets/qdesktopwidget.h>
#endif
#include <QtGui/qguiapplication.h>
QT_BEGIN_NAMESPACE
class QDesktopWidget;
Obviously fail on the forward declaration.
Below is qt.swigcxx:
// See swig.org for more inteface options,
// e.g. map std::string to Go string
%module qt
%{
#include <QPushButton>
#include <QApplication>
%}
%ignore "";
%include <qapplication.h>
%include <qpushbutton.h>
How can I change so that swig omits the forward declaration?
If you really need to inform SWIG it should ignore something in the source code, you can use the macros it defines: Documentation
So your qapplication.h changes to this:
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qcoreapplication.h>
#include <QtGui/qwindowdefs.h>
#include <QtCore/qpoint.h>
#include <QtCore/qsize.h>
#include <QtGui/qcursor.h>
#ifdef QT_INCLUDE_COMPAT
# include <QtWidgets/qdesktopwidget.h>
#endif
#include <QtGui/qguiapplication.h>
QT_BEGIN_NAMESPACE
#ifndef SWIG
class QDesktopWidget;
#endif
Be careful with this tactic in general, though, you might end up doing something to make the SWIG preprocessor succeed, but then fail the C++ compilation because you left out something important.
Another approach is to avoid %include altogether (speaking from enterprise experience here). As your project grows large, SWIG will increasingly slow down as a result of including everything from your header files. In the end, just copying the declarations SWIG needs is worth it for the compilation times.

Error LNK 2005 Visual Studio 2008

Please help to build my project, i gave up after 2nd hour.
That is how it looks like:
BrickClass.h
- #include"header.h"
header.h
- #pragma once
- #include windows.h
- #include windowsX.h
- #include tchar.h
- #include commctrl.h
- #include "matrixClass.h"
- #include "resource.h"
mainClass.h
- #include "header.h"
- #include "brickClass.h"
matrixClass.h
- #include cstdlib
- #include cstdio
- #include math.h
brickClass.cpp
- #include "brickClass.h"
main.cpp
- #include "mainClass.h"
mianClass.cpp
- #include "mainClass.h"
What i need to do to make him happy?I was tried a lot of variants but cant figure out HOW..?
Appreciate your help.
The project is Here : http://www.filehosting.org/file/details/381812/Tetris.rar
If you're giving up after the 2nd hour, this probably isn't the right industry for you :-) There'll be times when you'll have spent days trying to solve a problem, kicking yourself at the end because it was so simple in retrospect.
Anyway, on to the problem at hand. It's almost certainly because you have code in your BrickClass header file.
By including that header file in both main.cpp (via mainClass.h) and BrickClass.cpp, each of the object files gets an independent copy of the code.
Then, when you try to link those object files together, the linker finds that there are two copies.
Header files should generally contain declarations, like extern int i; or function prototypes such as int xyzzy (void);.
The definitions, like int i; and functions such as int xyzzy (void) {return 42;}, should only be placed in the "regular" source files.