Visual Studio not recognizing certain classes - c++

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

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!

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.

C++ Weird error. Fails to compile

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!

MSVC 2008 Immediate Window nonsense and some code confusion

I've been fooling with the MSVC 2008 immediate window for the last few hours and I'm flabbergasted with both myself and Microsoft... It probably doesn't help that I stumbled on this mystery at bedtime and it's now 6 hours later. :)
Please see the following:
? "1234567\\87654321\\"
CXX0026: Error: bad format string
I've tried the above several ways in the immediate window and... Nothing. No amount of backslashes gets rid of the error. Removing the backslashes is the only way to solve it.
Does the expression evaluator have something against double backslash in a wide string?
For what it's worth, the immediate window fooling was motivated by the following:
Line 107 is:
size_t endpos = str.find_last_not_of( L”\\/” );
file.cpp(107) : error C2017: illegal escape sequence
file.cpp(107) : error C2017: illegal escape sequence
file.cpp(107) : error C2065: 'L”' : undeclared identifier
file.cpp(107) : error C2065: '”' : undeclared identifier
My questions are:
What's up with the 4 errors on line 107?
What's up with the immediate window? I remember this kind of thing working there a year or so ago. I applied a service pack to MSVC 2008 about 6 months ago but I didn't use it heavily until now.
size_t endpos = str.find_last_not_of( L”\\/” ); // no
size_t endpos = str.find_last_not_of( L"\\/" ); // yes
Beware of code that you copied off a website, maybe a blog post. The author may well have used a word processor, one that implements 'smart quotes'. If you look closely at the first and the second line you'll see the difference. Your compiler will only like the straight double-quotes.
It doesn't quite explain your problem with the Immediate Window, it works when I try your string as shown. Maybe it doesn't quite look like it either.