PDA

View Full Version : Cannot use created class in the XCode?




galimk
Apr 23, 2011, 02:49 PM
Hello everyone,

I am new to Objectvice C and just started learning it.

The situation that I cannot resolve is pretty trivial, I am sure there is something simple I am missing. I created Fraction.m class in my test project in XCode using Source -> Add -> New File ->Objectvie C Class. I filled the .h file with the members and implemented all of them in the *m file. Afterwards I am trying to instantiate this Fraction class in my Program.h class in the main function.

I tried to include the .h file through #import <Fraction.h> directive but I get compiler error "No such file ore directory". Also whenever I am trying to use Fraction class, I get compiler error that Fraction is not defined. Though intellisense in the XCode code editor is seeing both "Fraction.h" and Fraction class. I am sure that there are no compilation errors in the Fraction.h and Fraction.m.

Thanks.



chown33
Apr 23, 2011, 03:19 PM
I tried to include the .h file through #import <Fraction.h> directive but I get compiler error "No such file ore directory".

Look in your book for exactly what you're supposed to use for #import. You may have made a mistake.

I'm not sure, but #import <Fraction.h> seems wrong to me. If Fraction.h were a system-provided class, it would be correct. But it's a developer-created class, so #import "Fraction.h" seems more sensible to me, and is what I've always used when referring to class headers I create.


I am sure that there are no compilation errors in the Fraction.h and Fraction.m.
Post your code.

No one with any programming experience would believe anyone (beginner or experienced) who says there are no compilation errors, unless the code has actually been compiled without errors. You may be right, but we need to see the evidence of the compiler.

galimk
Apr 23, 2011, 05:30 PM
Look in your book for exactly what you're supposed to use for #import. You may have made a mistake.

I'm not sure, but #import <Fraction.h> seems wrong to me. If Fraction.h were a system-provided class, it would be correct. But it's a developer-created class, so #import "Fraction.h" seems more sensible to me, and is what I've always used when referring to class headers I create.



Post your code.

No one with any programming experience would believe anyone (beginner or experienced) who says there are no compilation errors, unless the code has actually been compiled without errors. You may be right, but we need to see the evidence of the compiler.

Thanks! Your answer helped. So now I know that <**> are only for system provided classes. I used quotes for Fraction.h and it worked.
The example that I used had everything dumped in the main .m file along with the main function.

chown33
Apr 23, 2011, 06:53 PM
Thanks! Your answer helped. So now I know that <**> are only for system provided classes. I used quotes for Fraction.h and it worked.
The example that I used had everything dumped in the main .m file along with the main function.

I'm glad you got it working, but please be specific in the future about what you did, and what the book told you to do. Accuracy is important in programming. Books are specific about what to do because the author knows what works and what doesn't, and has given accurate instructions on what to do that works.

If you're working from a book, and they show you what to put into a file, you should put exactly that into a file. So if your book showed you to put everything dumped into the main.m file, then that's what you should do.

However, if they showed you something else, then dumping everything into a single main.m file is asking for trouble, because you don't know all the consequences of what you're doing.

If the book showed "Fraction.h", then that's what you should use. If they showed <Fraction.h> and it didn't work, then that's an error in the book.

Do not make changes to what a book tells you unless you fully understand what the consequences are. And if you're a beginner, then almost by definition, a beginner does not fully understand all the consequences. If you did understand all the consequences, you wouldn't be a beginner.

KnightWRX
Apr 23, 2011, 07:10 PM
Your answer helped. So now I know that <**> are only for system provided classes.

Actually, <include.h> is for headers that are found in the -I include paths specified in the CFLAGS var usually with make. You could add -I/path/to/your/headers to your GCC command line and then #include <yourheader.h> just like any other "system" include. #include "yourheader.h" includes headers either with an absolute or relative path to the current working directory of the compiler, which when using a make based system, happens to be your source directory.

Just some extra info there for you.