Understanding Errors

The thing that consistently stumps me the most are esoteric compiler errors that Xcode (or is is really gcc) displays. I constantly refer back to this page in my wiki. There are a lot of reasons why these errors can appear, more than what I have listed here, but these are the ones I bumped up against time and time again while I was getting my bearings in Xcode. Sources aren’ recorded, but that’s okay because most of the summaries were gleaned from a lot of reading.

expected specifier-qualifier-list before ‘(class)’

This can mean that the class in question either didn’t have its header imported or it wasn’t forward declared (with @class). This can also means that there is a cyclical dependency and a forward declaration needs to be used to resolve the dependency. Please see Tim Burrell’s Effective C Cyclical Dependencies.

Forward Declarations

Essentially, with forward declarations, you’re telling the compiler that you want to use a class without knowing anything about it. In compiler terminology basically you’re doing namelookup, and that’s about it. You can’t use any objects of that type until you give the compiler the details it needs. This is fine though, because you can make sure to only use the variable after its class definition is available. Here’s an example:

NetworkServer.hpp (.h)

#include "NetworkConnection.hpp"

class NetworkServer {
public:
	int someFuncA() { return 0; }
private:
	list mConnections;
};

NetworkConnection.hpp (.h)

This is where the forward declaration is first made.

@class NetworkServer; // <-- Forward declaration

class NetworkConnection {
public:
	NetworkConnection(const NetworkServer& server);
private:
	NetworkServer& mServer;
};

NetworkConnection.cpp (.m)

Note this is the implementation, not the header.

#include "NetworkConnection.hpp"
#include "NetworkServer.hpp" // The #include is done here instead

NetworkConnection::NetworkConnection(const NetworkServer& server) : mServer(server) {
	mServer.someFuncA();
}

Source: Not recorded, and it certainly isn’t mine.

statically allocated instance of Objective-C class ‘(class)’

Forgot the memory pointer token (*) before the variable name associated with a class.

request for member ‘(variable)’ in something not a structure or union

  • A header is imported more than once in all the classes used somewhere.
  • In a class-forward declaration, the class was declared in the header (@OCFooObj;) but not in the implementation (#import “OCFooObj.h”)

initializer element is not constant

When implementing a constants class that isn’t used as a class, you can’t have a const declaration whose initialization depends upon another (const) variable. In other words, the following is only valid in an actual class object, not just a simple constants header and implementation file:

UIColor * const kNewsNavigationBarColor = [UIColor blueColor];