operator keyword in Xcode

Go To StackoverFlow.com

1

I'm facing a problem in Xcode with the operator keyword. I have this in a header file (.h) but Xcode recognizes it as a keyword due to operator overloding in C++, even though I'm setting the file type as a default C header and not C++ header. The code I'm trying to compile is all written in C and the line of code i'm having problem is like this

struct foo {

    int a;
    int b;
    ...

    char operator[80];
}

I'm stuck with this a while now. The reason for the operator to be used as a variable name is not in scope now becouse this is a cross plataform code that I can't change and is compilable in many other plataforms for low-level devices.

Any help will be appreciated.

2012-04-04 20:14
by Raphael Ayres
I don't understand why changing the variable name to another thing would be a problem. If it compiles now, it should compile with the name changed, even for low level devices - EmilioPelaez 2012-04-04 20:27
The problem is that I'm responsible for porting this to iPhone and so, there are some modifications that I have to make within the common files in order for it to compile, and so, I would have to change this definition, placing an #ifdef IPHONEOS at the top of it and replace it in every file using this struct. I don't give support to this code, it's another team, so they advised me not to touch the logic nor the variable names, only includes and native function calls - Raphael Ayres 2012-04-04 20:31
Can you show us the error message? Is it from the compiler or from the IDE? (I haven't used Xcode, so don't assume I know what I'm talking about.) If your code is being compiled as C++ rather than C, there could be other subtle problems; there are constructs that are valid C and valid C++, but with different semantics (and operator isn't the only C++-specific keyword). Figuring out how to compile as C is much better than working around the problem by changing the identifier - Keith Thompson 2012-04-04 20:39
The error was about expecting a type after operator keyword - Raphael Ayres 2012-04-04 21:01
@RaphaelAyres: You're paraphrasing the error message. Please show us the exact error message (copy-and-paste it), and indicate whether it was produced by the IDE or during compilation - Keith Thompson 2012-04-04 21:47


1

How about if you used the pre-processor:

#define operator _operator

? Put the above in a header file that is read in before your header.

2012-04-04 20:33
by Owen Hartnett
And use an identity definition #ifndef IPHONEO - Owen Hartnett 2012-04-04 20:34
Didn't work. It seems that the compiler recognizes the operator keyword even in the #define. It does not give any errors, but it does not replace the keyword. Thanks anyway - Raphael Ayres 2012-04-04 20:41
@RaphaelAyres this works for me. The compiler highlighting the text is inevitable (as you use it inside a struct), but it compiles to the right code in the end - Richard J. Ross III 2012-04-04 20:46
Ok, it worked. With plain C files it worked without the #define, but with cpp files or mm files the #define did the job. Thanks - Raphael Ayres 2012-04-04 21:00
@RaphaelAyres: That's probably a usable workaround, but it's not a real solution. You're trying to compile C code; you should be able to persuade Xcode to compile it as C. With that workaround, you're just compiling C++ code that looks like C - Keith Thompson 2012-04-04 21:46


0

The file type that matters is the file you're including the header in. If you're trying to include this header in a C++ file then this error is expected and there's no way to use this header from C++. If you're already using it only in C files then I'm not sure off the top of my head what the issue could be.

2012-04-04 20:17
by bames53
The file is not being included in any sources that are included in the build. I don't get it. I have C++ files within this project but they're also not in the build. Actually, any .h that I write "operator" will recognize as a keyword - Raphael Ayres 2012-04-04 20:25
@RaphaelAyres: If the file isn't being included by anything in the build, why is it there - Keith Thompson 2012-04-04 20:40
I wish I knew. The project is so complex and has so many files, that I wouldn't be amazed with this happening. I think it is reading all the files in the project folder. But I will have to use it at some point in the project anyway - Raphael Ayres 2012-04-04 20:43
@RaphaelAyres If it's not included anywhere then what is the problem? You can't be getting build errors or anything like that if the header isn't included. Is it just syntax highlighting you want fixed - bames53 2012-04-04 21:29
Ads