views:

195

answers:

3

Hello,

I am porting a project to the iPhone system and I am facing the following problem: I have an header containing c++ templates If I rename it to .mm, it does not compile (because it should be an header) and if I keep it as .h, it is interpreted as an objective C header

Do you have a workaround to fix this issue?

Thanks in advance!

Regards,

edit: add error message and code

template <typename T>
class CML_Matrix
{
public:
//rest of teh template
};

error message is "cannot find protocol declaration for typename"

+1  A: 

Rename it to .hpp.

(That header have to be #include'd by a .cpp or .mm file. The extension of the header means nothing besides giving a hint to the IDE what language it is using.)

KennyTM
Thanks for your answer. However, I still have an error with templates : "cannot find protocol declaration for typename"
AP
Edit that into to your question and *add the code that is causing it*.
Georg Fritzsche
That header have to be `#include`'d by a `.cpp` or `.mm` file. The extension of the header means nothing besides giving a hint to the IDE what language it is using.
KennyTM
+3  A: 

Wrap it in

#ifdef __cplusplus
//templates here
#endif

This way, the templates will be silently ignored when the file is included in a C or Objective C (.m) source.

You can also have some Objective C-only constructs wrapped in

#ifdef __OBJC__

EDIT: you can, alternatively, rename your sources (not the header!) to .mm. Since it's a mixed ObjC/C++ project, you'll probably have to instantiate/call C++ classes at some point; for that, you'll need Objective C++ anyway. Never tried this, though.

Seva Alekseyev
Thanks for your answer. I still have a doubt: I have an template class defined in an header file wrapped in a #ifdef __cplusplus//templates here#endif bloc.How can I use it in an objective c application?Thanks in advance!
AP
You cannot use C++ templates in Objective C - templates are not a language feature in Objective C. In Objective C++, however, they are. Rename your sources to .mm and instantiate away. I just tried that on a a toy iPhone project - renamed main.m to main.mm, and it compiled fine. Even with #ifdef __cplusplus around the template definition.
Seva Alekseyev
+2  A: 

Make sure the files including the C++ header are being compiled as either C++ or Objective-C++. Objective-C files (with extension .m) will not compile C++ successfully. Changing their extensions to .mm should resolve your issue.

fbrereto