AP.
AP.

Reputation: 5323

c++ templates in an iPhone project

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"

Upvotes: 1

Views: 1558

Answers (3)

fbrereto
fbrereto

Reputation: 35925

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.

Upvotes: 4

Seva Alekseyev
Seva Alekseyev

Reputation: 61351

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.

Upvotes: 5

kennytm
kennytm

Reputation: 523244

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.)

Upvotes: 2

Related Questions