objc++ problem
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;
};
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;
};
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?
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...
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.
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.
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?
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.
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.
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!
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.
But that's also a blanket solution.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| How do you create enums for state charts in ObjC? | riruilo | 7 | 5,790 |
Mar 27, 2010 11:50 PM Last Post: AnotherJake |
|

