objc++ problem

Nibbie
Posts: 4
Joined: 2009.07
Post: #1
Hello, I have been googling all day and I just cannot seem to find the answer. I have a simple c++ class in a .mm file and it compiles just fine. But when I try to use the class (when i import the header in another file) it gives me this error. I really want to just be able to use objc++...
If anyone could help it would be much appreciated!

Here is the error:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Test'

Here is the class:
class Test
{
public:
Test()
{
num = 5;
}

int getNum()
{
return num;
}
private:
int num;
};
Quote this message in a reply
⌘-R in Chief
Posts: 1,254
Joined: 2002.05
Post: #2
Never seen that particular error before so I'm not super sure what is causing it, but is the file you're #import'ing this header into also an .mm file?
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #3
No, its not. Its a .h. Does it have to be a .mm? Are there any compiler settings or anything that need changed to get c++ to work? Thats the only thing I could come up with...
Quote this message in a reply
Member
Posts: 161
Joined: 2005.07
Post: #4
That's the class, but what's in the rest of the file before the class definition? Typos can cause compile errors further down in a file.

I'm not familiar with that specific error so I don't know if that'd be the reason. It's the only thing I can think of, though.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #5
Thats all thats in the file. I am under the impression I can just use c++ classes anywhere throughout the project. Is that not the case?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
To understand the problem, you need to understand how the compiler and #include/#import work.

The compiler compiles only the source files of your project (.c, .m, .cc/.cpp, .mm). While compiling these files, other files may be #include/#imported.

The language to be compiled is determined by the extension of the source file, and all the files that are #included must be valid for that language.

What you have happening is (almost certainly), your C++ header file being #included by a .c or .m source file, where C++ isn't valid.

The "easy" fix is to rename all your source files to .mm, where both C++ and ObjC are valid. It's not necessarily the *right* fix though.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2009.07
Post: #7
Thank you very much OneSadCookie. That did the trick. I dont know if there is a better fix or not, but at least this can get me started. Thank you everyone!
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #8
The easy fix is to change the "Compile Source As" build setting so that it always compiles as Objective-C++.

But that's also a blanket solution. Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do you create enums for state charts in ObjC? riruilo 7 6,893 Mar 27, 2010 11:50 PM
Last Post: AnotherJake