Initialize union using largest member under MSVC compiler - c++

I'm trying to initialize a LARGE_INTEGER to 0 in a C++ library (C++03 to be exact). Previously, the initialization was:
static LARGE_INTEGER freq = { 0 };
Under MinGW it produced a warning:
missing initializer for member '_LARGE_INTEGER::::HighPart'
So I changed the initialization to the following in accordance with Can a union be initialized in the declaration?:
static LARGE_INTEGER freq = { .QuadPart = 0 };
I'm now testing under Visual Studio 2015, and its producing an error:
81 static LARGE_INTEGER freq = { .QuadPart = 0 };
82 if (freq.QuadPart == 0)
83 {
84 if (!QueryPerformanceFrequency(&freq))
85 throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed ..."));
86 }
hrtimer.cpp(81): error C2059: syntax error: '.'
hrtimer.cpp(81): error C2143: syntax error: missing ';' before '}'
hrtimer.cpp(82): error C2059: syntax error: 'if'
hrtimer.cpp(83): error C2143: syntax error: missing ';' before '{'
hrtimer.cpp(83): error C2447: '{': missing function header (old-style formal list?)
hrtimer.cpp(87): error C2059: syntax error: 'return'
How do I initialize a union to its largest member under the MSVC compiler?
Here is Microsoft's definiton of LARGE_INTEGER:
#if defined(MIDL_PASS)
typedef struct _LARGE_INTEGER {
#else // MIDL_PASS
typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
} DUMMYSTRUCTNAME;
struct {
DWORD LowPart;
LONG HighPart;
} u;
#endif //MIDL_PASS
LONGLONG QuadPart;
} LARGE_INTEGER;

{ .QuadPart = 0 }; is illegal in C++. Designated initializers are C-only, however, they are supported from C++20. You link to a c question.
In C++03 [dcl.init.aggr]/15: (your union is an aggregate):
When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer for the first member of the union.
So, it is not possible to initialize "the largest member" unless that member is the first member.
The MinGW warning is bogus. g++ used to issue warnings for = { 0 };, however that is a common idiom, so they fixed it to not do that any more. I guess you have a slightly old version.
In your code, = { 0 }; should initialize DUMMYSTRUCTNAME to {0, 0}. According to this, all members of your union are 64-bit so in this particular case, you did actually initialize the largest member.

Related

c++, syntax error : missing ';' before identifier 'N0'

I have an issue compiling my code on windows.
On Unix-based systems all is working fine, but when I compile it on windows (currently with visual studio 2010 express), I'm getting the following errors:
Error 253 error C2146: syntax error : missing ';' before identifier 'N0' C:\ghost++\ghost\ohconnect.h 45
Error 254 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 45
Error 255 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 45
Error 256 error C2146: syntax error : missing ';' before identifier 'N' C:\ghost++\ghost\ohconnect.h 46
Error 257 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C:\ghost++\ghost\ohconnect.h 46
And so on.
I think it's all relating to my header file, the class itself is made to connect to websockets:
#ifndef OHConnect_H
#define OHConnect_H
//
// OHCONNECT
//
class CTCPClient;
class CBaseGame;
class CCommandPacket;
struct OHCHeader {
unsigned header_size;
bool fin;
bool mask;
enum opcode_type {
CONTINUTATION = 0x0,
TEXT_FRAME = 0x1,
BINARY_FRAME = 0x2,
CLOSE = 8,
PING = 9,
PONG = 0xa,
} opcode;
uint64_t N0;
uint64_t N;
uint8_t masking_key[4];
};
In my .cpp file I'm using namespace std; and included <string> only for windows.
But all this didnt work so far. I didnt wanted to put the whole files into the question since they are actually long.
Here is the full source:
Headerfile
Mainfile
What did I wrong here?
The compiler doesn't know the type uint64_t and uint8_t, add:
#include <cstdint>
Note also that the trailing comma in the enum (after the definition PONG = 0xa) was only standardized in C++11, following the change made in C99. Older compilers or those running in a mode following the older 1998/2003 standard may trip over that as well.

syntax errors in C++

I'm new in c++. my friend just gave this code to me but it doesn't work and sends many syntax errors like : error C2146,error C2734,... which I'm not familiar so I thought it should be better to ask stack overflow.
pixel.cpp:
#include<iostream>
#include"a.h"
using namespace std;
extern const unsigned uint_8 microsoftSansSerif_8ptBitmaps[];
extern const unsigned FONT_INFO microsoftSansSerif_8ptFontInfo;
extern const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[];
int main()
{
getchar();
}
a.h :
// Font data for Microsoft Sans Serif 8pt
const unsigned uint_8 microsoftSansSerif_8ptBitmaps[] = {
0b11110000,
0b00010000,
0b00101000,
0b00101000,
0b01000100,
0b01000100,
0b01111100,
0b10000010,
0b10000010,
};
const FONT_CHAR_INFO microsoftSansSerif_8ptDescriptors[] =
{
{7, 0}, // A
};
const FONT_INFO microsoftSansSerif_8ptFontInfo =
{
2, // Character height
'A', // Start character
'A', // End character
2, // Width, in pixels, of space character
microsoftSansSerif_8ptDescriptors, // Character descriptor array
microsoftSansSerif_8ptBitmaps, // Character bitmap array
};
errors :
a.h(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
a.h(2) : error C2146: syntax error : missing ';' before identifier 'microsoftSansSerif_8ptBitmaps'
a.h(3) : error C2059: syntax error : 'bad suffix on number'
a.h(3) : error C2146: syntax error : missing '}' before identifier 'b11110000'
a.h(4) : error C2059: syntax error : 'constant'
Some problems:
uint_8, FONT_INFO, and FONT_CHAR_INFO are not declared anywhere.
Unnecessarily adding unsigned before custom types is an error.
Binary literals require compiling in c++11 mode, and a compiler which supports that.
To use getchar, you need to include <cstdio>.
Generally, you should put the extern declarations in the .h file and the definitions in the .cpp, not the other way around.

initializing variable in different-different position in windows is giving error

I am initializing simple int variable in my code but it gives some unwanted errors... If I use integer(or other data type) variable in some places it gives error. I write down my code and put comment where integer variable is showing error.
#include<stdio.h>
#include<Windows.h>
//int i; ///********* no problem ************
int main()
{
//int i; ///********* no problem ************
STARTUPINFO si;
PROCESS_INFORMATION pi;
//int i; ///********* no problem ************
ZeroMemory(&si,sizeof(si));
//int i; // error C2143: syntax error : missing ';' before 'type'
si.cb=sizeof(si);
//int i; //error C2143: syntax error : missing ';' before 'type'
ZeroMemory(&pi,sizeof(pi));
//int i; //error C2143: syntax error : missing ';' before 'type'
if(CreateProcess("C:\\Windows\\System32\\notepad.exe",NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi))
{
//int i; ///********* no problem ************
printf("process created\n pid is=%d tid is=%d\n",pi.dwProcessId,pi.dwThreadId);
}
else
{
//int i; ///********* no problem ************
printf("process creation error\n");
}
// int i; // error C2143: syntax error : missing ';' before 'type'
}
I am using cl.exe compiler and visual studio 2012.I am compiling code from command line
cl process.c
The Visual Studio compiler doesn't support C99, which you are attempting to use.
You must use just C90, i.e. keep your variable declarations at the top of their containing scope.
You must declare all your variables just after the { of a function.
This is because Visual Studio supports C89 and C89 forbids mixed declarations. In C99 and later,variables can be declared anywhere

why type casting on non-pointer struct give syntax error

I am using Visual C++ express 2008 try to compile code similar to below:
no problem
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008
It seem like the same code will be compiled with no error/warning on gcc compiler. Why I get this error on VC++? Is it a C/C++ standard way to do casting on struct value? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
Use av_get_time_base_q() instead of AV_TIME_BASE_Q for C++ or VS.
This was fixed in a patch
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options:
Rename your program with a .c suffix
Switch on the /TC flag for the program that does not compile.
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.
For compound-literals errors in C++
wrong:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);

What's the typedef syntax for an array type?

I have this C++11 code:
using swallow = int[];
but MSVS2013 Preview barfs on it:
error C2143: syntax error : missing ';' before '='
So I tried
typedef int[] swallow;
But that got me:
warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
So I tried reversing the typedef stuff, as I never remember (hence the reason using is so great):
typedef swallow int[];
And got:
m:\development\source\ambrosia\libambrosia\Ambrosia/utility.h++(33) : error C2144: syntax error : 'int' should be preceded by ';'
I'm already disappointed in MSVS2013. How can I write this so the MS compiler will understand this simple code?
typdef is a declaration, and follows the same syntax as a declaration:
extern int a[];
typedef int b[];
(Note that b is an incomplete type, and that a is only declared, not defined.)