Strange behavior with compile error "expected specifier-qualifier list before 'CustomClass'" - objective-c

I have been having some trouble lately with using custom classes as types. As described in the title, I have been getting compile errors similar to the one below:
expected specifier-qualifier list before 'MyClass'
My code is usually something along the lines of this:
#interface MyCoolClass : NSObject {
MyClass *myClassObject; // Error is on this line.
}
I also occasionally use something like this:
#interface MyCoolClass : NSObject {
IBOutlet MyClass *myClassObject; // Error again on this line
}
Im not really sure if that is good to use but on occasion, I have done something like that so I could connect my objects together with Interface Builder so that one object could invoke a method on another object.
I've been able to do this before successfully with other projects but when I tried to do it today, it gave me this error. Any ideas what could be causing it? As far as I can tell, I have done the same thing that I did in the other project here.
It is also to my understanding that this error usually gets thrown if the type is not defined, but I am pretty sure that I have defined it.

Oh, GCC how obtuse and opaque can your errors possibly be....
Try compiling with the LLVM 2.0 compiler. It'll give you much more sane errors.
In this case, what is usually going on is that the compiler doesn't have a clue what MyClass is or there is a syntax error in the previously included header file that doesn't cause a compilation error until the #interface is hit in the file spewing the error.
It could also be a misspelling.
Or, as suggested, you need to #import "MyClass.h" into the header file (or implementation file or, even better, the PCH file) so that MyClass is defined before the iVar declaration.
#class MyClass;
That'll also do the trick.

Related

xcode 4.6.1 and LLVM 4.2: ld: 2 duplicate symbols for architecture armv7

My first attempt at building a company iOS library/framework was this week, by following the steps found at this blog post here.
For reasons beyond this question, I can only link when building for a Device and not for simulator.
However, now I am getting a very bizarre error:
ld: 2 duplicate symbols for architecture armv7 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
The lines in question suggest:
duplicate symbol _OBJC_CLASS_$_iContactsGridCell in:
/Users/*/Desktop/Projects/contactservice/branch/ContactServicesClient/DerivedData/iContacts/Build/Intermediates/iContacts.build/Debug-iphoneos/iContacts-5.1.build/Objects-normal/armv7/iContactsGridCell.o
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/athium-iOS.framework/athium-iOS
duplicate symbol _OBJC_METACLASS_$_iContactsGridCell in:
/Users/*/Desktop/Projects/contactservice/branch/ContactServicesClient/DerivedData/iContacts/Build/Intermediates/iContacts.build/Debug-iphoneos/iContacts-5.1.build/Objects-normal/armv7/iContactsGridCell.o
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/athium-iOS.framework/athium-iOS
Obviously the Class in question is iContactsGridCell.h
The class itself inherits from Cell.h
#interface iContactsGridCell : Cell
The class Cell.h is part of the framework, if I do not import the correct file, then as expected I get a semantic error: Cannot find interface declaration for Cell etc etc.
However, when I do import it, I get the following duplicate error.
This file is not included anywhere else in the project, apart from iContactsGridCell.h!
The file Cell.h in turn, is just a class found under the framework:
#interface Cell : NSObject
#property (strong,nonatomic) UIView *view;
#property CGRect rect;
#property int UID;
#property BOOL highlighted;
#property UIColor *b_colr;
- (id) initWithRect:(CGRect)frame;
- (BOOL) hasCoordinates:(CGPoint)coord;
- (void) ripple;
- (void) cubefy;
- (void) flipfy;
- (void) rotate;
- (void) setBg:(UIColor *)bg withAlpha:(CGFloat)alpha;
- (void) highlight;
- (void) unhighlight;
- (void) updateWithRect:(CGRect)rect;
#end
Why on earth am I getting the duplicate error?
What could be causing this?
How could it get fixed?
How can I get more info more verbose output of where the duplicates are found?
PS: I have followed the intructions found at the blog to the letter. Yet I cannot link for simulator (getting a wrong architecture error) so my guess that maybe something is broken in the framework and not the project itself. Could this be the reason for the duplicate errors?
There are several places where this error could be arising. I would do the following to start searching for the problem:
Search the project folder with finder and see if anywhere else the file "iContactsGridCell.h" exists. Or if any two files exist somewhere.
Make sure that you don't have two Objects that are of Class iContactsGridCell that share the same name for example:
iContactsGridCell *myObj;
iContactsGridCell *myObj;
Make sure your not doing anything like this: #import
"iContactsGridCell .m"
Or like this: #import "iContactsGridCell.h" #import "iContactsGridCell.h"
Make sure your not re-declaring a class (including ones that Apple has provided in their frameworks)
Main Point: That's all I can think of off the top of my head. But the thing to remember is that somewhere in your project resides two objects, class declarations etc. That are the same. And the compiler is complaining because of it.
Why the Compiler would complain (more info): While this information is really "unnecessary" it's good practice as a programmer to understand what's going on under the hood and will often help debug, so here's a little info on compilers:
Remember that a Compiler (while much more complex) is a program just like the ones that you create using it. One of the steps in most (if not all) compilers go through at one point or another reading code is creating "Symbols or Keys" for each variable, class, struct ect. So, at some point the compiler reached a line of code that "repeats / duplicates" somewhere else. So the "Key/Symbol" creation process done routine by the compiler fails.

How do i interface my objc program with an objc++ library?

I have an objc program and i would like to use a widget that is written in objc++ (namely https://launchpad.net/scintilla-cocoa). How do i go about this? Basically i want a new window controller object to interface with this objc++ library to define a scintilla text editor widget. Simply creating a new 'objc class' and accessing the library from there generates a bunch of errors related to the C++ class keyword and so on.
Thanks in advance
Since I'm the one who put you into the (hopefully rewarding :-)) trouble of using Scintilla, here I am.
Let's say we create a ScintillaView subclass, named ppScintillaEditor.
The file should have an .mm extension (e.g. ppScintillaEditor.mm)
The code would be roughly like this...
Interface
#import "Scintilla/ScintillaView.h"
#interface ppScintillaEditor : ScintillaView
{
// your iVars
}
// your properties / methods / whatever
Now, as for the implementation part, remember to put some initialization method to set up the view properly (as in the example accompanying Scintilla-cocoa; I mean the Test project)
Sidenote : Of course, you can create subclasses, categories or whatever on top the ScintillaView class, pretty much based on what you need - I, for example, have create a separate Category just in order to group there some ScintillaView specific commands (sooner or later, you'll notice that for some more advanced Scintilla manipulations, although it's there, it may need some polishing to be a bit more cocoa-friendly, so here you go...)
Now, last but not least...
To resolve the "bunch of errors related to the C++ class keyword and so on", as I've shown in my other video-response to your comment, all you have to do is :
Go to your project's Build Settings
Under Apple LLVM Compiler 3.0 - Preprocessing
Option Preprocessor Macros
Add to both Debug and Release :
SCI_NAMESPACE SCI_LEXER
And that's it. :-)
Hint : The above are defined by Scintilla to avoid clashes between C and non-C elements, like above... so, all it takes is to notify the preprocessor and the rest is taken care of....
you would create an objc class which has the interface your app needs, then implement and add the ivars and implement -- all behind a compilation firewall so the objc++ sources are not included in the header. your implementation would provide any necessary conversions.
it is like you have already done, but you remove the scintilla headers from the header for your wrapper -- they are visible only to your wrapper's implementation.
Update
To illustrate one possible approach:
MONScintillaWrapper.h
// no c++/scintilla sources should be included in this header
#import <Foundation/Foundation.h>
#interface MONScintillaWrapper : NSObject
- (void)setBackgroundColor:(NSColor *)pColor;
#end
MONScintillaWrapper.mm
#import "MONScintillaWrapper.h"
#implementation MONScintillaWrapper
{
scintilla::t_thing scintillaThing;
}
- (void)setBackgroundColor:(NSColor *)pColor
{
...convert pColor to a scintilla color and pass that to scintillaThing...
}
#end

missing #end + expected identifier "{" errors in XCode 4.2 .h module

I can't get rid of two errors on my class module, even when I have simplified the code to the minimum expression:
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
#end
Both errors are reported on the #interface line, and they are:
- missing #end
- expected identifier or '{'
Check the header files that are #imported on the same page, and verify that the headers have matching #interface/#end statements.
I had the same problem, and XCode reported that my .m file's #implementation had a missing #end statement, even when I pared it down to just:
#import "MasterViewController.h"
#import "MyBadHeaderFile.h"
#implementation MasterViewController
#end
In reality the bug was a missing #end from a #imported MyBadHeaderFile.h file.
I had a similar problem found adding a } in my code got rid of the errors for me.
Before I added the } I was getting the following errors:
unexpected # in program (parse issue)
Expected } (parse issue)
#end is missing in implementation context (semantic issue)
rokjarc's comment should be a correct answer:
1) compiler can point you to completely wrong file. go trough all .h and .m files in your project and check for matching '#interface'/'#end', '#implementation'/'#end' and so on.
2) it happens also if you by mistake import .m file instead of .h
It doesnt matter in which file the error is appearing. Make sure to check for all the files you have recently modified. I appeared to have copied a file from test project to my actual project and forgot #end in one of the files. It gave me error on totally unrelated .h file.
Try to remove the implementation file of this class from "Compile sources in project settings"
#interface MyClass : NSObject{
}
#end
I experienced a similar issue, the code was fine but it was throwing this error.
I eventually resolved it by cleaning the project, have you tried that?
PEBKAC + Lack of Specificity in the Static Analyzer Strings
The existing answers are certainly useful. I just want to add a common PEBKAC that causes this; as well as highlight that CLANG should be clearer here. Someone with domain knowledge should file a patch to make the error message clearer. The \n#end\n part of the message is ridiculous no matter what.
(Showing line breaks as \n in a message designed to help users of a
GUI? Really CLANG?)
The Cause
When you have just created a new Class, the #interface & #implementation files look extremely similar, and Xcode will automatically place your cursor in the implementation.
If you thoughtlessly begin typing out your interface (your method signature declarations) in the implementation file, you'll end up with this warning because the analyzer sees methods without opening and closing braces.
A visual example, shown forthwith
I had the same error ,and it was because i had
#interface MyViewController ()
declared two times in the MyViewController.m

Strange error regarding instance variables & superclass

I've got some code where my classes inherit from a superclass, and everything has been working fine till now. I'm getting an error whenever I try to use any of the superclass variables, saying that they are undeclared (first use in this function). It's only happening in one of my subclasses, & it looks exactly the same as the others. I'm wondering if there's anything obvious which I should know about (being quite new to Objective-C). The basic code is like -
#interface mySuperClass : UIViewController {
BOOL myVar;
}
Then -
#interface mySubClass : mySuperClass {
}
#implementation mySubClass {
-(void)someMethod {
myVar = YES; // error here
}
#end
Any help much appreciated - if you need more info, let me know! Thanks.
I just got over a very similar strange error where I could no longer access properties in my superclass, and xcode was givine me compiler errors saying "(*) undeclared (first use in this function)". However I had not any problems in the past...
The problem was that I had introduced typos at the top of my .m file and the xcode compiler output was misleading me. Specifically I had #synthesize statements where the properties were misspelled, either in the synthesize statement or in the corresponding variable in the headerfile.
If you have #synthesize statements or other declarations, examine them with a fine toothed comb (i.e. which lines have you introduce most recently?), or even comment out a block of them to see if you can compile again and narrow down the culprit.
Again the compiler errors were very misleading, so it really was tough to debug. Although like 99.9% of the time the error was my own. :)
I can't see anything wrong with the code you've pasted. My first guess would be that you're not importing the mySuperClass .h file properly. Ie you're missing
#import "mySuperClass.h" //or #include
In mySubClass.h, or mySubClass.m
I ran into the same problem and after wasting hours I finally solved it in my project:
I refactored my source and removed a member but forgot to remove the #property definition in .h file. This leads to errors at every place when I am using super members. BTW: Methods from super are fine.
So check your property defs. Thanks to Screwtape in post 8 for his solution :-)
EDIT: Oops, Kris' answer is pretty similar except from typos vs. removal.
I am having the same issue for weeks
"Variable Undeclared" error when compiling to iOS Device, but not for Simulator
One solution I found is to simply change the compiler from the default LLVM GCC 4.2 to LLVM Compiler 2.0 (or to `Apple LLVM Compiler 2.1). It seems to be a bug in the compiler, but it is just a guess.
Changing it is a quick fix for your problem if there is no need for you to use the GCC compiler at all.

Objective-C #import loop

I have the following code:
#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"
#protocol ServerRequestDelegate<NSObject>
-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;
#end
It compiles and runs fine. However, if I replace the method declarations with:
-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;
I get the unexpected syntax error "error: expected ')' before 'ServerRequest'". The only reason I can think this might be a problem is that ServerRequestDelegate.h and ServerRequest.h #import each other. However, I don't understand why the code works with the #import line with (id)request. I also don't understand why it's a syntax error.
Can someone provide a good explanation?
You've already hinted at the explanation: an #import cycle.
The first thing I'd do is remove the #include and add the following line above the #protocol definition:
#class ServerRequest;
This is a forward class declaration, and can help break the import loop. Check out this SO question for more details. Apple also has a brief explanation in this guide.
Basically, #import'ing a file causes the compiler to bring the entire text of that file into the file in question, and although #import is "smarter" than #include, it doesn't mean you're immune from import errors. The #class declaration is a way to tell the compiler that a class exists without importing the header. It's appropriate to use when you only need to know about the class name, but don't care about the methods it provides. Generally, you want to use #class in the .h file and #import in the .m file, where you're actually interacting with the class.
#import "loops" are not a problem. #import is the same as #include except that it tracks files and makes sure the preprocessor only reads them in the first time.
Usually when you get an error like that it is due to a problem in the included file. So the error is probably in ServerResponseRecord.h, thought it is probably being tripped by actually using the object declared by it. Without seeing the complete headers it is not possible to say exactly what is going on.