Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Declaration does not declare anything?



Hello, all these warnings and errors happen because Objective C extends C and
not C++.


On 09.04.2008, at 07:39, Mark Teagarden wrote:
Hi,

My code looks like this:
--------------------------------
reader.h:
#import <Cocoa/Cocoa.h>

@interface Reader: NSObject {
	struct FileHeader {
		int x;
	};

	struct VarHeader {
		int x;
	};

The warnings "does not declare anything" relate to these two structure declarations.
An interface declaration is not like a class declaration in C++, usually it just declares
instance variables and methods. Just move the two 'struct ...' declarations before @interface
and the warnings will disappear.




	struct FileHeader fh;
	struct VarHeader vh[1000];
}

-(void)Read;

@end
----------------------------------
reader.m:
#import "reader.h"

@implementation Reader
-(void)awakeFromNib {
	[self Read];
}

-(void)Read {
	int i;
	FILE* fp;
	
	fp = fopen("test.xxx","r");
	fread(&fh,sizeof(FileHeader),1,fp);

In C, a structure declaration does not define a new type. You need to use either
'sizeof(struct FileHeader)' or 'sizeof fh'. Many people prefer the second variant, since
it allows to change the type of fh later on.



... } ------------------------------------ Which gives me the following warnings and errors:

w: declaration does not declare anything (occurs after #import "reader.h")
e: "FileHeader" undeclared (first use this function) (after the fread statement)


I know that this code is supposed to work because it comes straight from the author. There must be something simple I'm overlooking, because it looks to me like I HAVE declared these things in the header. Is there some quirk about structure declarations in Obj-C? This code was originally in a .cpp file but I don't see anything here that should cause difficulties. If I replace sizeof(FileHeader) with sizeof(fh), the program will compile, although it still gives me the declaration warnings.

As already said, the difference between .c and .cpp actually is a (small) problem here.
If you really need C++, you might also rename reader.m to reader.mm and use Objective C++.
By the way, the usual convention is to name the .m, .mm and .h files like the class they
define. So you should probably rename your files to Reader.h and Reader.m, respectively.


Hauke Klein

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden
References: 
 >Declaration does not declare anything? (From: Mark Teagarden <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.