1

I'd like to use a C++ stack type in Objective-C, but I'm running into some issues. Here's a sample of what I would like to do:

#import <stack>
#import <UIKit/UIKit.h>

@interface A : NSObject {
    stack<SEL> selectorStack;
}

@end

Unfortunately, this doesn't compile. After messing around with the code for a while and trying different things, I can't seem to find a way to accomplish this. Can somebody tell me the best way to use a C++ stack within an Objective-C object or if it's even possible?

Thanks.

UPDATE:

Well, KennyTM's answer worked on my example file, but for some reason when I tried to rename the class it quit working. Here's the code I have right now:

#import <stack>
#import <UIKit/UIKit.h>

@interface MenuLayer : NSObject {
    std::stack<SEL> selectorStack;
}

@end

The compiler spits out the following errors:

stack: No such file or directory
expected specifier-qualifier-list before 'std'
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
  • 2
    Make sure you haven't accidentally renamed it ending `.m` instead of `.mm`. Otherwise the C++ headers aren't in the header search paths (and you can't use C++ of course). – Georg Fritzsche May 06 '10 at 08:11
  • I double checked it, and the extension is .mm. Just to be clear though, the interface is located in a header file. Does that make a difference? – LandonSchropp May 06 '10 at 09:03
  • 1
    If the header is included in some plain Objective-C (`.m`) file you end up with the same problem. See e.g. [here](http://stackoverflow.com/questions/2262011/adding-c-object-to-objective-c-class/2262395) for alternatives. – Georg Fritzsche May 06 '10 at 09:06
  • The opaque pointers worked perfectly. Thanks gf. If you were to copy your answer into a reply then I'll give you the check mark. Thanks again. – LandonSchropp May 06 '10 at 17:14
  • 1
    Added it for future reference, but give the check-mark to KennyTM - he solved the problem as originally posted. I only fixed the follow-up. – Georg Fritzsche May 07 '10 at 13:07

2 Answers2

2

Have you tried

@interface A : NSObject {
    std::stack<SEL> selectorStack;
}
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Hey, thanks for the quick reply. I feel kind of stupid for not specifying the fully qualified class name. When I changed this on my sample file, it fixed the problem. However, I tried changing the name of the class to something else and the compiler spit out a couple of new errors. I updated my post with more information. Thanks again. – LandonSchropp May 06 '10 at 08:09
1

For the second problem make sure you haven't accidentally renamed the file ending .m instead of .mm. Otherwise the C++ headers aren't in the header search paths (and you can't use C++ of course).

Also, if the header is included in some plain Objective-C file (.m) you end up with the same problem. You could avoid this e.g. by using opaque pointers.

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236