0

I need to use C++ file in my project, it's called CAXException.hpp, and row in project targets "Compile Sources As" - "According to file type" not Objective-C++. But when it's compiling it always displays me error error: expected '=', ',', ';', 'asm' or '__attribute__' before 'CAXException' in code:

class CAXException //<-------error here

{}

Please help me to fix it..

LightNight
  • 1,616
  • 6
  • 30
  • 59
  • 1
    Which one is it, objective C or C++? – Seth Carnegie Aug 31 '11 at 14:15
  • [This answer](http://stackoverflow.com/questions/525609/use-c-with-cocoa-instead-of-objective-c/529017#529017) has a lot of information about what you need to do to use C++ with Objective C. And the other answers on that same page should pretty helpful too - regardless of the flame war in the comments. – Mike Hay Aug 31 '11 at 14:15
  • If you're following convention, an .hpp file is included as a header file in an other translation unit. Please edit your question to tell us a bit more about *that* file: is it C, C++, Objective-C, or Objective-C++? How are you including the .hpp file? – Barry Wark Aug 31 '11 at 16:00

3 Answers3

2

If I understand you correctly, you have a C++ header file (.hpp) that you want to include from Objective-C file. Unfortunately, you can't do that directly. You'll have to use a workaround.

The easiest is to change the compilation option of each and every Objective-C file (.m) that include this C++ header file (either directly or indirectly) to be compiled as an Objective-C++ file. This can be done either by renaming the files to .mm extension or by changing the option for the compiler for the file.

If this work for you, this will be the easiest, however Objective-C++ is not a complete superset of Objective-C (as C++ is not a superset of C), and some valid Objective-C is invalid Objective-C++ (if C++ keywords are used as variables names).

If this happens, you'll have to create an Objective-C wrapper to the class, with an implementation in Objective-C++ that simply delegate to the C++ class. That is create an CAXExceptionWrapper.h Objective-C file, containing something like:

@interface CAXExceptionWrapper {
  @private
    void* _CAXExceptionImpl;
}
- (id)init;
// ...
@end

And an `CAXExceptionWrapper.mm' Objective-C++ file containing:

@import "CAXException.hpp"
@implementation CAXExceptionWrapper
- (id)init {
    if ((self = [super init])) {
        _CAXException = new CAXExceptionWrapper;
    }
    return self;
}
// ...
@end

And then in your Objective-C files, include the wrapper Objective-C header instead of the C++ header.

Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85
1
class CAXException //<-------error here
{};
 ^^^

you are missing the ;

And Your compiler seems not to recognize the C++ keyword class, which is strange. Most likely, you are missing some ; before this class definition or some other syntax error but before this.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @mazder the error is before that line. Most likely you are also missing a `;` at the end of a class declared above that one. – Seth Carnegie Aug 31 '11 at 14:14
  • When I set in project targets "Compile Sources As" - "Objective C++" this file compiles normal, just about 100 other errors in other files (facebook connect and twitter libraries) – LightNight Aug 31 '11 at 14:16
  • Just fixed this problem, `#import` from header file (.h) can't be declared it's must be in the `.mm` file. – LightNight Aug 31 '11 at 14:45
1

It is possible that that header is being included from an objective-c (.m extension) source code file. The source code file that includes that header must be an objective c++ one, i.e. ends with .mm or you can force objective c++ by explicitly changing the "Compile sources as..." setting.

5ound
  • 1,179
  • 6
  • 9