Function prototype with default arg fails to compile

B

Bill Davidson

All:

I recently had to include a common header file into a straight C (.c file).
This header file contains a function prototype that has a default argument.
For example

int MyFunc(int MyArg = 10);

The compiler has no problems when I include this header file into a C++
(.cpp) source file, but croaks when I include it in a C (.c) source file.
The error(s) are as follows.

error C2143: syntax error : missing ')' before '='
error C2072: 'MyFunc' : initialization of a function

This isn't surprising since default arguments are a part of C++, not C.

My question: How can I compile this C module under C++ such that the header
file can successfully be compiled into this project?

Thanks,
Bill
 
W

William DePalo [MVP VC++]

Bill Davidson said:
This isn't surprising since default arguments are a part of C++, not C.

'Zactly Right.
My question: How can I compile this C module under C++ such that the
header file can successfully be compiled into this project?

The compilers accept switches to cause it to consider a file to be something
other than the default:

http://msdn.microsoft.com/library/d...y/en-us/vccore/html/_core_.2f.Tc.2c_2f.Tp.asp

You can use the compiler's property pages in the IDE to add a switch there,
too.

Regards,
Will
 
B

Bill Davidson

Thanks, Will ! That's the switch I was looking for, but couldn't seem to
find.
..
Cheers,
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top