Syntax error compiling header containing "char[]" - c++

I am trying to build a Visual C++ 2008 DLL using SDL_Mixer 1.2:
http://www.libsdl.org/projects/SDL_mixer/
This is supposedly from a build made for Visual C++, but when I include SDL_mixer.h I get error C2143: "syntax error : missing ';' before '['".
The problem line is:
const char[] MIX_EFFECTSMAXSPEED = "MIX_EFFECTSMAXSPEED";
Is this because of the use of the dynamic array construct "char[]", instead of "char*"?
All the expressions in the file are wrapped by "extern "C" {".

move the square brackets after the variable name
const char MIX_EFFECTSMAXSPEED[] = "MIX_EFFECTSMAXSPEED";

You want:
const char MIX_EFFECTSMAXSPEED[] = "MIX_EFFECTSMAXSPEED";
Note that there is no "dynamic array construct" here - you have an array of char that is initialised witha string literal - all compile time things.

My bad. Although the answers here are correct regarding C construct, the actual problem was that I had included a "D" language file instead of the C version.

Related

Delete Files with C++ on windows

I am writing a C++ program and I want to have some created files deleted if meeting with some conditions. These file have various file names that are assigned using type "string" in each iteration. Now I'm trying to delete some of the files with their file names, but it seems that neither Deletefile function nor remove can deal with C++ strings. I have also tried to convert the strings to c type char* but it doesn't work.
I'm using visual studio community 2015 on windows 10.
Is their any convenient way for this problem?
As said in the comment - the function .c_str() returns C-compatible string that can be use with DeleteFile and remove.
If that doesn't work, I'd guess that you app is compiled as Unicode , meaning you will have to use std::wstring instead of std::string.
try to combine the two ways:
std::string fileName = "C://file.txt";
std::wstring wFileName(fileName.begin(),fileName.end());
auto res = DeleteFile(wFileName.c_str());
remove however, uses "regular" const char*.

C++ Error C2440 when using exisiting C source

I am having an issue where I have a C2440 error when compiling a C++ project in VS2005. The error is due some existing C code in another project which I depend on which casts a void pointer to either a char or int pointer.
The code resembles:
void * bbb;
... // some code which defines the void pointer
int * aaa = bbb;
However in C++ I need to specifically cast the type to be valid such as:
int * aaa = (int *)bbb;
My question is whether there exists a flag or compile option in VS2005 which allows my to compile my main project in C++ and ignore this error from depending projects I want to compile as C?
I would rather not change any of the original source as it is a shared project.
If you name the file "something.c", the compiler will [unless explicitly told otherwise] compile it as C (and thus happily accept your pointer conversion without using a cast). A file named "something.cpp" will be compiled as C++, and you will need a cast to convert a pointer to a different type, even if it's a void pointer.
The only way to do this was to edit the header files which caused the problem. It turned out to be a minor number of files which could be merged back into the shared projects.
There is no way to get around this in VisualC++. I had used -fpermissive to ignore the error in gcc.
So I just made the update to:
int * aaa = (int *)bbb;

Weird syntax error in Xcode based on file type

I have a C header with this function:
OSStatus MyGetDataFromExtAudioRef(ExtAudioFileRef ext_file_ref, const AudioStreamBasicDescription* restrict output_format, ALsizei max_buffer_size, void** data_buffer, ALsizei* data_buffer_size, ALenum* al_format, ALsizei* sample_rate);
I can #include this header in an Objective-C file and compile fine.
If I change that same Objective-C file to a .mm suffix rather than .m and verify that it is now identified as Objective-C++ source, I get a compiler error that says Expected ')' on the line above. While not clear, it seems to be placing the source of the error on the word output_format parameter.
There is nothing in the above function that appears to be bad syntax, with regards to parenthesis, and I can't understand why this one change should make any difference.
Does anything jump out at anyone here?
Simple, the restrict keyword isn't part of the C++ standard, so the C++ compiler (that is used to compile Objective-C++ code) doesn't recognize it.

Oracle Pro*C Precompiler error PCC-S-02201

An existing program that is being converted to use Oracle Pro*C is causing problems upon precompilation. It reads a file from the filesystem, parses it, and writes to a couple of database tables.
There's a method with the following definition:
void parse_line(inline)
char *inline;
{
// do stuff
}
When I attempt to make it, I see:
Syntax error at line 162, column 13, file myfile.cp:
Error at line 162, column 13 in file myfile.cp
char *inline;
............1
PCC-S-02201, Encountered the symbol ";" when expecting one of the following:
( * const, volatile, an identifier,
This function declaration is syntactically correct as far as I can tell, so I have to assume that this precompiler error is coming up because of a problem elsewhere.
Short of pasting the entire program in here, does anybody have any suggestions as to a few good places I could start looking?
My pcscfg.cfg looks like this:
sys_include=($ORACLE_HOME/precomp/public,/usr/include,/usr/lib/gcc-lib/x86_64-redhat-linux/3.2.3/include,/usr/lib/gcc/x86_64-redhat-linux/4.1.1/include,/usr/lib64/gcc/x86_64-suse-linux/4.1.2/include,/usr/lib64/gcc/x86_64-suse-linux/4.3/include)
ltype=short
define=__x86_64__
That declaration is invalid since inline is a keyword in C and C++ (and can only be used as a function specifier in C).
Change that variable name to something else, and that should go through if you're compiling as C and not C++. I don't believe that style of function definition syntax is acceptable in C++.

Strange visual studio 2008 C++ compiler error

I have three lines of code:
//int pi;
activation->structSize = sizeof(rmsActivationT);
int pi; //program wont compile with this here
every time I uncomment the second int pi and comment the first int pi I get this error: syntax error : missing ';' before 'type'. When i uncomment this first int pi and comment the second int pi, my compiler doesn't complain anymore. This error has been bothering me for almost a full day now any ideas would be great.
Thanks
Visual studios 2008
Windows XP 32 bit
Are you, perhaps, compiling the code as C instead of C++? C (prior to C99, which Visual Studio doesn't support) required that all definitions in a block precede any other statements.
I had the same problem.
The compilation errors were:
*main.cpp(325): error C2601: 'FLAG' : local function definitions are illegal
main.cpp(323): this line contains a '{' which has not yet been matched
main.cpp(326): fatal error C1075: end of file found before the left brace '{' at 'main.cpp(323)' was matched*
But there was nothing wrong with my code. I counted all brackets and the number matched. There weren't any function inside another function.
I solved it by removing all "//" comments from the source code. It seems that the reason for that is bad line formatting which causes the compiler to miss a line break, so the line after a comment is treated as a comment as well.
For example:
// This is a comment
This_is_a_line;
is treated as:
// This is a comment This_is_a_line;