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

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

Related

How to resolve build errors while integrating gmime into VS2010 project?

I'm trying to use gmime in a vc++ project I'm working on.
I have gmime dependent dll, lib and header files.
Trying to build the first example from the tutorial fails horribly however.
This is the code
#include <stdio.h>
#define ssize_t long
#include <gmime/gmime.h>
int main (int argc, char **argv)
{
GMimeStream *stream;
/* initialize GMime */
g_mime_init (0);
/* create a stream around stdout */
stream = g_mime_stream_file_new (stdout);
/* 'printf' */
g_mime_stream_printf (stream, "Hello World!\n");
/* flush stdout */
g_mime_stream_flush (stream);
/* free/close the stream */
g_object_unref (stream);
return 0;
}
While build the vc++ project, I am getting following error,
Error C2061 syntax error: identifier 'GMimeStream'
Error C2059 syntax error: ''
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2143 syntax error: missing ';' before ''
Error C2086 'int ssize_t': redefinition
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int
Error C2086 'int GMimeStream': redefinition
Kindly help me to resolve this issue.

Initialize union using largest member under MSVC compiler

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.

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.

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);

Why C++ not know TIMESTAMP_STRUCT type?

I use Visual Studio 2005
When I compile, I get this error:
Error 1 error C2146: syntax error : missing ';' before identifier 'mDropEndTime'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
struct MB_SYN_DROPITEM_UPDATE : MSGBUF
{
long mCid; // Index
unsigned long mItemIdx; // idx
TIMESTAMP_STRUCT mDropEndTime; // This is error line
};
Why doesn't C++ know TIMESTAMP_STRUCT?
TIMESTAMP_STRUCT is something defined in sqlext.h
You must add
#include <sqlext.h>
Because TIMESTAMP_STRUCT is not part of the C++ standard.