stevemu

I have a C function that uses a CPP class as one of its arguments. When complied with VC8.0 Express, I got error like:

c:\ctrader\tsim202\trdsim2.hpp(22) : error C2061: syntax error : identifier 'TRDSIM'

c:\ctrader\tsim202\trdsim2.hpp(22) : error C2059: syntax error : ';'

c:\ctrader\tsim202\trdsim2.hpp(22) : error C2449: found '{' at file scope (missing function header )

c:\ctrader\tsim202\trdsim2.hpp(271) : error C2059: syntax error : '}'

However, I can complie the HPP header without any problem in a separate project. (Tried to build HPP lib separately.) I think the issue is including the HPP header in a C file.

How can I resolve this problem without re-program the C funtion If I create a HPP lib first then link it with the C funtion, how do I stop the re-compile of the HPP header in the C file Can I use pre-complied-header to resolved the issue, or what other option I have to change the project setting in order to make the C funtion compile

Thanks.



Re: Visual C++ Express Edition Using CPP class inside C function

Jonathan Caves - MSFT

A C function cannot use a C++ class - C has no understanding of what a class is. You need to reduce your class down to the level of a C struct and then use that in the C code. Maybe something along the line of:

// C consumable header

struct ReducedCLevelFunctionality {

};

// C++ header

#include "C consumable header"

class TRDSIM : public ReducedCLevelFunctionality {

};






Re: Visual C++ Express Edition Using CPP class inside C function

orcmid

stevemu wrote:

I have a C function that uses a CPP class as one of its arguments. [ ... ]

Can I use pre-complied-header to resolved the issue, or what other option I have to change the project setting in order to make the C funtion compile

If you compile the C function as C Language, it will reject language constructs (such as class definitions) as errors. You cannot use a C++ Class in a C function via use of the C Language. (Well, there are ways, but let's not talk about COM right now.)

Pre-compiled headers have nothing to do with it. That is a device for getting VC++ to compile faster -- it is a compensation for the gigantic use of headers for Win32 applications and does nothing to the language (although it can be a source of errors when moving projects around and reusing projects). Don't use it unless you have to - it makes your code non-portable.

If you compile the C function as C++, it can accept a C++ Class as a parameter and use it properly. Just change the name of the file from *.c to *.cpp, clean up any incompatibilities (there might not be any), and you are in business.

If you want the C function to still be callable from other C programs, you can do that by using extern "C" although I'm not sure this makes sense - no ordinary C program can provide a C++ class as the parameter.

Does this help

- Dennis

PS: If you don't have any methods on the C++ class that need to be used from the C function, the alternative that Jonathan just posted is also worth considering.

PPS: Now that I think about it, what could the C function (as opposed to a C-like function written in C++) be doing with a C++ class It is hard to imagine how a program originally written in C could be used with a C++ class as a parameter without any change.






Re: Visual C++ Express Edition Using CPP class inside C function

stevemu

Thanks Dennis! Compiled the C funtions in CPP, everything worked.