C++ Weird error. Fails to compile - c++

I have been using the allegro 5 libraries for developing a game in C++ for some time. Today I got some weird error:
I have a class called level. I have a header file called levelhandler.
Here's how it looks:
#pragma once
#include "level.h"
level level_1;
level *currentlevel;
void initialize_levels()
{
currentlevel = &level_1;
}
When I try to compile it gives me strange errors like:
error C2086: 'int level' redefinition
error C2143: syntax error : missing ; before 'level_1'
I remember that it could compile before, and I did use currentlevel->Player.X a lot of times, but now I have a lot of that and it gives errors like these:
error C2227: left of '->Player' must point to a class/struct/generic type
error C4430: missing type specifier - int assumed
header pasted from comment
#pragma once
#include "entity.h"
// some more includes
class level {
public:
enum Tileset { ... };
enum Tile { ... };
int tiles[200][200];
player Player;
level(void);
~level(void);
};

Such errors are hard to find as long as you look at the "Error List" pane. Select View/Output to show the "Output" view. The line after the error C2086 shows the original definition of level.
You fill find an
int level;
there as the C2086 tells you. If it's the line
level level_1;
of your fist example you will have to check the last header file include in your compilation unit. It might end with an int or it has a unbalanced #if clause.
To find the exact location start using a Short, Self Contained, Correct (Compilable), Example. This helps you to find the bug and saves time of other with their crystal balls.
Edit:
Another way to find the reason for this unexpected behavior is to see the preprocessor output. Set the Generate Preprocessed File option int the C/C++/Preprocessor project property page to With line numbers (/P) and look in the generated <sourcefile>.i
Check that that level.h file has included what you intended.

Ok, so I've been tinkering around with my project the weekend, and I finally found out what whas the problem that gave me so many weird compiler errors. It seems that I had a lot of cases where two header files were including each other, and the compiler really didn't like that, so I corrected that, and now I'm ok. Thank you all for helping me, and have a great day!

Related

Why can't Intellisense find variables from a header file when including it into a namespace?

First of all thank you for taking the time to read and maybe answer this.
I want to include a compressed font, which has been converted from a .ttf file into a .h file.
The compressed font is saved (in the .h file) inside an array of the type:
static const unsigned int font_name_compressed_data[about 3 - 10 thousand]
For what I am using (ImGui) this is all fine and I can import the font correctly.
The problem is, when i want to encapsulate those font_name_compressed_data arrays (one for each font) within an namespace, intellisense suddenly stops recognizing them.
It still compiles fine, but intellisense does not work and it constantly shows that there are still errors.
I am using c++17 although i don't think that matters.
And I am compiling with Visual Studio 2019, while editing with Visual Studio Code.
Did I do it wrong the first time or is this a bug from intellisense?
~~ E D I T :
My assumption was wrong, after manually opening every font_name.hpp file and individually adding the namespace to it, intellisense was working again. But after closing and reopening it, the same error came back. It is either this
namespace "Fonts" has no member "roboto_compressed_data"
or this
name followed by '::' must be a class or namespace name
seemingly random.
If I autocomplete Fonts:: in Visual Studio 2019 I get all my fonts, but if I do the same thing in Visual Studio Code I dont get any, unless I manually open the font header file in Visual Studio Code and close it again.
Also the error comes back after doing Rescan Workspace.
I think I have my project set up right, since I don't get an error anywhere else when using namespaces or includes.
Seems like VS Code just unloads the font header file from memory or something, since it is pretty long, but I don't know...
When i do it like this it does not get recognized:
in the compressed_font.h
static const unsigned int font_name_compressed_data[size of the array] =
{ 0x0000bc57, 0x00000000, 0x78b30000, ... and so on }
where i then want to use it (.cpp):
namespace font_data
{
#include "compressed_font.h"
}
// ...
// v--- This is then unrecognized
font_data::font_name_compressed_data
But after i changed everything to this it got recognized again:
in the compressed_font.h
namespace font_data
{
static const unsigned int font_name_compressed_data[size of the array] =
{ 0x0000bc57, 0x00000000, 0x78b30000, ... and so on }
}
where i then want to use it (.cpp):
#include "compressed_font.h"
// ...
// v--- This is now recognized
font_data::font_name_compressed_data

Sonarqube c++ syntax error skip __namespace

Thanks for taking the time to read my question. Sonar-scanner analysis is successful but these warnings are printed on the screen. The team, being very new to Sonar and I, not knowledgeable enough in C++, are both stumped.
Sonar-scanner message:
WARN: [/home/jenkins/workspace/Sonar/LIS/src/CORE/CCPARSE/lib/Factory.C:39]: syntax error, skip '__namespace'
for other files containing the namespace block, everything in the namespace block are displayed as syntax errors. Files without the namespace block are fine. The team needs the namespace blocks.
The actual code in Factory.C:
#include <CCPARSE/Factory.H>
namespace CCPARSE {
Needless to say, if I comment out the namespace line and its closing brace, the warning goes away.
The file is analyzed in Sonar though. I can see the file mentioned in the analysis as well as a calculation of duplicated code, etc.
Thanks to a wise gentleman on GitHub, I figured out the issue. namespace is a functionality of C++ and not of C . Once I renamed the files as .cpp instead of .C, the syntax error went away!

Eclipse Invalid arguments error when using gstreamer

Ok, so I want to use gstreamer library.
1. Situation
I have some code:
#include <gst/gstpipeline.h>
#include <gst/gst.h>
...
GstElement* pipe = gst_pipeline_new("PipeName");
Where gst_pipeline_new is declared in gstpipeline.h:
GstElement* gst_pipeline_new (const gchar* name) G_GNUC_MALLOC;
where non obvious "things" :) are defined somewhere in the system:
typedef struct _GstElement GstElement; // gstelement.h
typedef char gchar; // gtypes.h
#define G_GNUC_MALLOC __attribute__((__malloc__)) // gmacros.h
2. Problem
Since I use make for building I have no errors during compilation and linking. Program itself runs OK as well. However...
In Eclipse IDE I have the following error:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
_GstElement * gst_pipeline_new(const ? *)
' file.cc /path/to/file line 106 Semantic Error
I added all include directories which are specified in Makefile to eclipse project configuration (Project->Properties->C/C++ General->Paths and Symbols->Includes->C++). Of course it's a C++ project.
3. Question
How to get rid of that Eclipse error? I have no clue how to do this... And it drives me mad since now I use some legacy code and I have around 100 errors like this one.
So far I've tried:
casting either by reinterpret_cast<>() or C-like casting to const gchar*
adding typedef char gchar at the beginning of the file - before any other include!
including gtypes.h (gchar is defined there) - also before any other include
redeclaring `_GstElement gst_pipeline_new(const gchar* name)'
Nither of those helped...
To me it looks like Eclipse does not see the gchar type since it says that the candidate is _GstElement * gst_pipeline_new(const ? *) Where ? substitutes the real type. But I have no idea how to make (or event force :)) Eclipse to see it...
Most probably eclipse just doesn't know about your include paths (for this specific library) and complains about the unindexed types and declarations.
You can add them under 'Project->Properties->C++ General->Paths and Symbols'
If this doesn't help, you can also switch off semantic error checking (see Code Analysis), either in whole or for particular error types.
As g-maulik suggested, It seems that it was really an indexer problem. After increasing the indexer cache limits everything works fine.
Go to Window->Preferences->C/C++->Indexer tab cache limits and increase (might be machine dependent):
Index Database cache:
Limit relative to the maximum heap size: 15%
Absolute limit: 128 MB
Header file cache:
Absolute Limit: 128 MB

Strange Error C2065: 'ERROR' : undeclared identifier

As part of a bigger project ( the ff activex plugin for Mozilla Firefox) there is this code snippet:
if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != ERROR)
{
::InvalidateRgn(m_hWndParent, hrgnClip, fErase);
}
When I build in VS2012 I get "Error C2065: 'ERROR' : undeclared identifier"
ERROR is defined in wingdi.h like this:
...
/* Region Flags */
#define ERROR 0 // it wont build when this macro is used
#define NULLREGION 1 // it builds when this macro is used
#define SIMPLEREGION 2
#define COMPLEXREGION 3
#define RGN_ERROR ERROR
...
The strange thing is that if I replace ERROR (just to see if it builds OK) with NULLREGION or SIMPLEREGION (which are macros in the same file, just two lines below the offending one) in the if statement above, the code builds OK. When I use ERROR, the code wont build.
Is it possible that the ERROR macro defined above gets masked by some keyword or another macro or some such by Visual Studio?
The problem here is that ERROR actually appears in the compile error message. That should not happen, the preprocessor should have substituted it with 0.
So somewhere in a .h file you #included after windows.h, some programmer took the 10 second shortcut to a macro name collision problem and wrote this:
#undef ERROR
You'd need to find that line and remove it. That's likely to be difficult and liable to give you a maintenance headache since that's a file you don't own and might well be updated in the future, forcing you to make that change over and over again. The alternative is to redefine it yourself:
#define MYGDIERROR 0
...
if (CombineRgn(hrgnClip, hrgnClip, hRGN, RGN_AND) != MYGDIERROR)
//etc...
Which still gives you a maintenance problem and requires you taking a bet that the return value definition of CombineRgn() is never going to change. That's a very safe bet, GDI is cast in stone.

Visual Studio not recognizing certain classes

I keep getting the error:
error C2146: syntax error : missing ';' before identifier 'mCameraFrame'
for the line of code:
Frame mCameraFrame;
So clearly my frame class isn't being found somehow. I have the frame.h header file (which defines the Frame class) directly included in this file. Why doesn't visual studio recognize it?
The error is coming from previous lines of code, possibly in a header file.
For example:
struct foo
{
int a;
}
Frame mCameraFrame;
Notice the missing ; after the }? That makes the Frame legal as an instance of the structure, but now there's a missing ; before mCameraFrame, resulting in the kind of error you reported.
The compiler can't report a missing ; after the } because it has no way to know there's supposed to be one there, since the Frame that comes after it is perfectly legal.
It's not unusual for a single missing ; or missing } to result in errors reported many lines later than the actual problem, sometimes hundreds of them.
Figured I'd report back to anyone that's interested. The problem was that the Frame class that was supposed to be definining mCameraFrame was in a different namespace, so all I had to do was "using namespace ....;". Doh! :P