C2061 Syntax Error (identifier) - c

1>cb.c(51): error C2061: syntax error : identifier 'SaveConfiguration'
1>cb.c(51): error C2059: syntax error : ';'
1>cb.c(51): error C2059: syntax error : 'type'
1>cb.c(52): error C2061: syntax error : identifier 'LoadConfiguration'
1>cb.c(52): error C2059: syntax error : ';'
1>cb.c(52): error C2059: syntax error : 'type'
1>cb.c(122): error C2061: syntax error : identifier 'SaveConfiguration'
1>cb.c(122): error C2059: syntax error : ';'
1>cb.c(122): error C2059: syntax error : 'type'
1>cb.c(127): error C2061: syntax error : identifier 'LoadConfiguration'
1>cb.c(127): error C2059: syntax error : ';'
1>cb.c(127): error C2059: syntax error : 'type'
1>
1>Build FAILED.
It's just a single .c file in the project. Here's the code:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <tchar.h>
typedef struct _Configuration
{
int KeyActivate;
int BlockWidth;
int BlockHeight;
double HueStart;
double HueEnd;
double SaturationStart;
double SaturationEnd;
double ValueStart;
double ValueEnd;
} Configuration;
typedef struct _DIBSection
{
HDC ScreenDC;
HDC WindowDC;
HDC MemoryDC;
HBITMAP ScreenBMPHandle;
BITMAP ScreenBMP;
} DIBSection;
typedef struct _Thread
{
HANDLE Handle;
unsigned Id;
} Thread;
typedef struct _Window
{
HANDLE Handle;
HDC DC;
int Width;
int Height;
int Top;
int Left;
} Window;
__declspec ( dllexport ) int Initialize ( void );
unsigned __stdcall Start ( void * Arguments );
void LoadDefaultConfiguration ( Configuration * Config );
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath );
bool LoadConfiguration ( Configuration * Config, LPTSTR FilePath );
Thread MainThread;
Window Screen;
Configuration Settings;
BOOL WINAPI DllMain ( HINSTANCE Instance, DWORD Reason, LPVOID Reserved )
{
switch ( Reason )
{
case DLL_PROCESS_ATTACH:
// TODO: Load settings from file
LoadDefaultConfiguration ( & Settings );
// Create main thread
MainThread.Handle = (HANDLE) _beginthreadex (
NULL,
0,
Start,
NULL,
0,
& MainThread.Id
);
if ( MainThread.Handle )
{
SetThreadPriority ( MainThread.Handle, THREAD_PRIORITY_BELOW_NORMAL );
}
else
{
MessageBox ( NULL, L"Unable to create main thread; exiting", L"Error", MB_OK );
ExitProcess ( 0 );
}
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
__declspec ( dllexport ) int Initialize ( void )
{
return 1;
}
unsigned __stdcall Start ( void * Arguments )
{
return 1;
}
void LoadDefaultConfiguration ( Configuration * Config )
{
Config->BlockHeight = 50;
Config->BlockWidth = 100;
Config->HueEnd = 0.00;
Config->HueStart = 0.00;
Config->KeyActivate = VK_SHIFT;
Config->SaturationEnd = 0.00;
Config->SaturationStart = 0.00;
Config->ValueEnd = 0.00;
Config->ValueStart = 0.00;
}
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath )
{
return true;
}
bool LoadConfiguration ( Configuration * Config, LPTSTR FilePath )
{
return true;
}
Line 51 is
bool SaveConfiguration ( Configuration * Config, LPTSTR FilePath );

bool is not a C type.
I do suspect BOOL is defined somewhere.
Same goes for the usage of true and false.

Actually, bool is a valid type (well, a macro actually) in the C99 standard, assuming you are using a recent compiler. You need to add:
#include <stdbool.h>
Note that bool is not valid in the older ANSI, C89, C90 etc variants of the C standards.
As highlighted by JeremyP in the comments, Microsoft's C compiler still lacks proper support for C99 features.
Which leaves three alternatives:
Treat it as C++, not C; because C++ has bool as a built-in type
Create your own bool implementation
Re-write the code to avoid using bool
For option 2 something like this would work, but it's an ugly work-around:
typedef short bool;
#define true 1
#define false 0

Related

error C2061: syntax error : identifier 'VAULT_ELEMENT_TYPE'

Here is my c code "vault.c"
#include "main.h"
HMODULE vaultcli_addr;
typedef HANDLE HVAULT;
#define VAULT_ENUMERATE_ALL_ITEMS 512
enum VAULT_SCHEMA_ELEMENT_ID {
ElementId_Illegal = 0,
ElementId_Resource = 1,
ElementId_Identity = 2,
ElementId_Authenticator = 3,
ElementId_Tag = 4,
ElementId_PackageSid = 5,
ElementId_AppStart = 0x64,
ElementId_AppEnd = 0x2710
};
enum VAULT_ELEMENT_TYPE {
ElementType_Boolean = 0,
ElementType_Short = 1,
ElementType_UnsignedShort = 2,
ElementType_Integer = 3,
ElementType_UnsignedInteger = 4,
ElementType_Double = 5,
ElementType_Guid = 6,
ElementType_String = 7,
ElementType_ByteArray = 8,
ElementType_TimeStamp = 9,
ElementType_ProtectedArray = 0xA,
ElementType_Attribute = 0xB,
ElementType_Sid = 0xC,
ElementType_Last = 0xD,
ElementType_Undefined = 0xFFFFFFFF
};
typedef struct _VAULT_BYTE_BUFFER {
DWORD Length;
PBYTE Value;
} VAULT_BYTE_BUFFER, * PVAULT_BYTE_BUFFER;
typedef struct _VAULT_ITEM_DATA {
DWORD SchemaElementId;
DWORD unk0;
VAULT_ELEMENT_TYPE Type; //Line 44
DWORD unk1;
union {
BOOL Boolean;
SHORT Short;
WORD UnsignedShort;
LONG Int;
ULONG UnsignedInt;
DOUBLE Double;
GUID Guid;
LPWSTR String;
VAULT_BYTE_BUFFER ByteArray;
VAULT_BYTE_BUFFER ProtectedArray;
DWORD Attribute;
DWORD Sid;
} data;
} VAULT_ITEM_DATA, * PVAULT_ITEM_DATA;
msvc compiler gives the error:
1>vault.c(44): error C2061: syntax error : identifier 'VAULT_ELEMENT_TYPE'
1>vault.c(60): error C2059: syntax error : '}'
1>vault.c(65): error C2061: syntax error : identifier 'PVAULT_ITEM_DATA'
1>vault.c(66): error C2061: syntax error : identifier 'Identity'
1>vault.c(66): error C2059: syntax error : ';'
1>vault.c(67): error C2061: syntax error : identifier 'Authenticator'
1>vault.c(67): error C2059: syntax error : ';'
I am compiling as C code, i can't figure out the error.
code is taken from here: https://github.com/twelvesec/passcat/blob/master/passcat/libvaultie.cpp
Please help me point out the error. The error starts on line 44.
struct and enum types have their own namespace, so you have to use struct TypeName and enum TypeName instead of just TypeName.
enum VAULT_ELEMENT_TYPE { ... };
enum VAULT_ELEMENT_TYPE vet;
Alternatively you could create a type using typedef.
typedef enum { ... } VAULT_ELEMENT_TYPE;
VAULT_ELEMENT_TYPE vet;
Nothing stops you from creating both.
enum VAULT_ELEMENT_TYPE { ... };
typedef enum VAULT_ELEMENT_TYPE VAULT_ELEMENT_TYPE;
enum VAULT_ELEMENT_TYPE vet; // ok
VAULT_ELEMENT_TYPE vet; // Also ok
You can even create both at once.
typedef enum VAULT_ELEMENT_TYPE { ... } VAULT_ELEMENT_TYPE;

Error with expected operator before token

The error is telling me I'm missing an operator somewhere, but I simply cannot see it so I figured some fresh eyes could help me find it.
Code snippet:
static int min_val, max_val;
struct arrNum
{
int charged;
int count;
};
static struct arrNum nums[];
static int max_num = 0;
static void sort_order(int iNum)
{
if (iNum < 0)
return;
if (iNum > max_num)
max_num = iNum;
struct arrNum nums[iNum].charged = 1;
struct arrNums nums[iNum].count++;
return;
}
Errors:
mergeSort.c: In function 'sort_order':
mergeSort.c:32:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNum nums[iNum].charged = 1;
^
mergeSort.c:32:29: error: expected expression before '.' token
mergeSort.c:33:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNums nums[iNum].count++;
^
mergeSort.c:33:30: error: expected expression before '.' token
Any help is welcome. Thank you!
here is a version of the code with comments:
// following two statements will cause the compiler to raise
// warning messages because this statements
// declare the variables, but they are never used
static int min_val;
static int max_val;
// define a struct with two fields
struct arrNum
{
int charged;
int count;
};
// declare an instance of a pointer to a struct
// actually want an array of `struct arrNum`
// so need a number between the '[' and ']'
// it must be given a size that is at least 1 greater
// than the highest value of 'iNum'
static struct arrNum nums[];
static int max_num = 0;
// following line will raise a compiler warning
// because 'static' function can only be referenced in the current file
// and nothing in the posted code calls it.
static void sort_order(int iNum)
{
if (iNum < 0)
return;
if (iNum > max_num)
max_num = iNum;
// note: both following statements are
// writing into 'la la land'
// because all that has been declared for
// 'nums[]' is a pointer
// and because it is declared 'static'
// is initialized to 0
// so executing either of these statements
// will result in a seg fault event
nums[iNum].charged = 1; // nums[] already declared, so don't declare it again
nums[iNum].count++; // nums[] already declared, so don't declare it again
}

Compile error when creating packet

I'm studying THIS tutorial for tinyos and I wanted to try it out. I try to create the packet but it gives me the following error. I don't know what's wrong. It is probably something simple but I can't figure out what it is.
#include "TestMsg.h"
...
event void AMControl.startDone(error_t error) {
if (error == SUCCESS) {
call Leds.led0On();
//create packet
TestMsg_t* msg = call Packet.getPayload(&packet, sizeof(TestMsg_t));
msg->NodeID = TOS_NODE_ID;
//
// //TODO in the meantime this can change
// button_state_t val = call Get.get();
// msg->Data = ( val == BUTTON_PRESSED ? 1 : 0 );
//
// //send packet
// if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
// radioBusy = TRUE;
// }
} else {
call AMControl.start();
}
}
...
Here is TestMsg.h
#ifndef TEST_MSG_H
#define TEST_MSG_H
typedef nx_struct _TestMsg {
nx_uint16_t NodeID;
nx_uint8_t Data;
} TestMsg_t;
enum {
AM_RADIO = 6
};
#endif /* TEST_MSG_H */
Here is the part where it is declared in the video
The error I get it this:
In file included from /home/advanticsys/ws/TestRadio/src/TestRadioAppC.nc:5:
In component `TestRadioC':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc: In function `AMControl.startDone':
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:43: syntax error before `*'
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: `msg' undeclared (first use in this function)
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: (Each undeclared identifier is reported only once
/home/advanticsys/ws/TestRadio/src/TestRadioC.nc:44: for each function it appears in.)
Update
Something is wrong with structs and headers.
#include "Szar.h"
#include "BarType.h"
module SzarP {
uses interface Boot;
uses interface Leds;
}
implementation {
event void Boot.booted() {
// TODO Auto-generated method stub
call Leds.led0On();
Szar_t foo;
Szar_t *szar = &foo;
BarType_t barVar;
barVar.data = 0;
BarType_t *pBarVar = &barVar;
pBarVar->data = 1;
}
}
Here are the 2 header files.
#ifndef SZAR_H
#define SZAR_H
typedef nx_struct _Szar {
nx_uint8_t szar1;
nx_uint16_t szar2;
} Szar_t;
#endif /* SZAR_H */
#ifndef BAR_TYPE_H
#define BAR_TYPE_H
typedef struct _BarType {
uint8_t id;
uint32_t data;
} BarType_t;
#endif /* BAR_TYPE_H */
And the errors:
In file included from /home/advanticsys/ws/Szar/src/SzarAppC.nc:6:
In component `SzarP':
/home/advanticsys/ws/Szar/src/SzarP.nc: In function `Boot.booted':
/home/advanticsys/ws/Szar/src/SzarP.nc:15: syntax error before `foo'
/home/advanticsys/ws/Szar/src/SzarP.nc:19: `barVar' undeclared (first use in this function)
/home/advanticsys/ws/Szar/src/SzarP.nc:19: (Each undeclared identifier is reported only once
/home/advanticsys/ws/Szar/src/SzarP.nc:19: for each function it appears in.)
/home/advanticsys/ws/Szar/src/SzarP.nc:20: syntax error before `*'
/home/advanticsys/ws/Szar/src/SzarP.nc:21: `pBarVar' undeclared (first use in this function)
For some strange reason I have to declare EVERY variable outside the function, and then it works. Example:
bool radioBusy = FALSE;
message_t packet;
TestMsg_t *messageToSend;
button_state_t buttonState;
event void AMControl.startDone(error_t error) {
if (error == SUCCESS) {
call Leds.led0On();
messageToSend = call Packet.getPayload(&packet, sizeof(TestMsg_t));
messageToSend->NodeID = TOS_NODE_ID;
//TODO in the meantime this can change
buttonState = call Get.get();
messageToSend->Data = ( buttonState == BUTTON_PRESSED ? 1 : 0 );
//send packet
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(TestMsg_t)) == SUCCESS) {
radioBusy = TRUE;
}
} else {
call AMControl.start();
}
}
It also works if I declare my variables at the beginning of the functions/events/commands without any code before them.

'this' undeclared (first use in this function)

I am newbie in C ,with Mingw32 compiler.
Right now i am creating decompiler from IL to C (Native)
The code generated (Without System.Object):
DecompileTestApplication_Program.c
#include "DecompileTestApplication_Program.h"
DecompileTestApplication_Program* DecompileTestApplication_Program__ctor( ) {
if (array__DecompileTestApplication_Program == 0) {
array__DecompileTestApplication_Program=(void**)malloc(sizeof(void*)*(capacity__DecompileTestApplication_Program=4));
}
DecompileTestApplication_Program* this;
//error: 'this' undeclared (first use in this function)
if (count__DecompileTestApplication_Program==0) {
this=(DecompileTestApplication_Program*)malloc(sizeof(DecompileTestApplication_Program));
goto RealConstructor;
}
this=(DecompileTestApplication_Program*)array__DecompileTestApplication_Program[--count__DecompileTestApplication_Program];
RealConstructor:
this->ind = 0;
this->a = 1;
this->b = 3;
//this._inherit_object_( ); //this is OOP tests ,still working on it
return this;
}
void DecompileTestApplication_Program_Main( ) {
int var_0_02;
var_0_02 = 0;
var_0_02 = ( var_0_02 + 1 );
int var_1_08;
var_1_08 = 1;
int var_2_0A;
var_2_0A = 3;
var_1_08 = ( var_1_08 + var_2_0A );
var_0_02 = ( var_0_02 + ( var_1_08 + var_2_0A ) );
DecompileTestApplication_Program_blat = ( DecompileTestApplication_Program_blat + ++DecompileTestApplication_Program_bpa );
}
void DecompileTestApplication_Program__cctor( ) {
DecompileTestApplication_Program_blat = 1;
DecompileTestApplication_Program_bpa = 4;
}
DecompileTestApplication_Program.h
#ifndef DecompileTestApplication_Program
#define DecompileTestApplication_Program
/*
Type's Name: DecompileTestApplication.Program
Time to Parse: 40.0023ms
*/
#include <stdio.h>
typedef struct {
//Variables
int ind;
int a;
int b;
} DecompileTestApplication_Program;
static int DecompileTestApplication_Program_blat;
static int DecompileTestApplication_Program_bpa;
//Methods
void DecompileTestApplication_Program_Main( );
DecompileTestApplication_Program* DecompileTestApplication_Program__ctor( );
void DecompileTestApplication_Program__cctor( );
static int count__DecompileTestApplication_Program=0;
static int capacity__DecompileTestApplication_Program=0;
static DecompileTestApplication_Program** array__DecompileTestApplication_Program=0;
#endif
#main.h
void main();
#main.cpp
//bookmart for includes
#include "DecompileTestApplication_Program.h"
void main() {
//bookmark for initialize
DecompileTestApplication_Program__cctor();
DecompileTestApplication_Program_Main();
}
The error found in the first file.
I searched the resolve for this error for awhile ,
but didn't found any.
#define DecompileTestApplication_Program
That means that everywhere you see the word DecompileTestApplication_Program, it gets removed. As such, your attempted declaration of this:
DecompileTestApplication_Program* this;
expands to
* this;
which attempts to dereference the undeclared variable this. To fix this, change the macro name.

Errors C2059 and C2061 in C language [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does VS2010 give syntax errors when syntax is correct?
I'm trying to develop a kind of Windows 32 service in C language, using Visual Studio 2010.
I created a new project, and inserted .c files :
main.c
service.c
misc.c
I also have two header files :
myerrors.h
my.h
Here's the code I have (be aware that it's just a draft).
main.c :
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "my.h"
#include "myerrors.h"
static int parse_args(int ac, char **av)
{
int i = 0;
while (++i < ac)
if (strcmp(av[i], "-i") && !InstallMyService())
return false;
else if (strcmp(av[i], "-d") && !UninstallMyService())
return false;
else if (strcmp(av[i], "-p"))
if (!av[i + 1])
return false;
else
{
if (!InsertPathInRegistry(av[i + 1]))
return false;
i++;
}
else
return false;
return true;
}
int main(int ac, char **av)
{
HANDLE hLogFile;
if ((hLogFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
aff_error(CANT_CREATE_FILE);
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(BAD_ARGUMENTS);
return EXIT_FAILURE;
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{DC_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
getchar();
if (!CloseHandle(hLogFile))
aff_error(CLOSE_FILE_FAILED);
return EXIT_SUCCESS;
}
misc.c :
#include <Windows.h>
#include <stdio.h>
#include "my.h"
#include "myerrors.h"
void aff_error(char *error_str)
{
fprintf(stderr, "ERROR: %s\n", error_str);
}
bool InsertPathInRegistry(char *path)
{
printf("LOG: Inserting %s as ", path);
}
void WriteInLogFile(HANDLE hLogFile, char *log_string)
{
printf("WriteInLogFile function");
}
service.c :
#include <Windows.h>
#include "my.h"
bool InstallMyService()
{
return true;
}
bool UninstallMyService()
{
return true;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
}
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
}
My headers are just some function declarations and macros such as :
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
my.h
#ifndef _MY_H_
# define _MY_H_
#include <Windows.h>
#include <strsafe.h>
/* Macros */
# define LOG_FILE_PATH "c:\\my_log_file.txt"
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
/* SERVICE functions */
void WINAPI ServiceMain(DWORD ac, LPTSTR *av);
bool InstallMyService();
bool UninstallMyService();
bool InsertPathInRegistry(char *path);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
#endif /*!MY_H_ */
While trying to compile the project, i got some weird errors :
my.h(19): error C2061: syntax error : identifier 'InstallMyService'
my.h(19): error C2059: syntax error : ';'
my.h(19): error C2059: syntax error : ')'
Or :
my.h(21): error C2061: syntax error : identifier 'InsertPathInRegistry'
my.h(21): error C2059: syntax error : ';'
my.h(21): error C2059: syntax error : 'type'
I checked on some forums that says those errors are commonly errors with includes badly placed, but I don't really know in this case, I don't think I made mistakes with includes...
Can anyone illuminate me ?
Thanks.
bool is not a data type in ANSI C. It is a data type in the C99 version of the language, only if <stdbool.h> is included, but Visual Studio does not support C99, only C89 (C99 also adds the _Bool data type, which can be used without including any headers).
I suggest you replace bool with another type such as int, or use a typedef to alias it with int or unsigned char or something.

Resources