• Multiple file problem/forward declarations
    3 replies, posted
Background: I have a year-ish of experience programing from a class and through a book/lots of internet searching for c++. From scratch, I've been trying to make a program that uses a character array to simulate pixels on a screen. Theres two classes so far, Point and Screen. [url=http://rapidshare.com/files/253273296/Graphics_Program.rar.html]I compressed with 7zip, rapidshared here[/url]. So anyways, Point functions use a Screen object called screen, and Screen functions use points. I only need one Screen object, and I want it to be globally used. I've been experimenting with the use of extern variables, and forward declaration things, but with my current build, I am getting a ton of errors that say: [code] forward declaration of 'struct Point' invalid use of undefined type 'struct Point' [/code] As well as an occasional [code]'a' has incomplete type[/code]Where a is of class Point. If you don't mind could you go through my code to find generally what I need to do to get every working smoothly? If you want, you can delete/comment out code that isn't written well, as long as the inclusions crap/variables/whatever is working all right. Also, a lot of things are commented out and slightly messy because I've been moving things around to find an answer.
-snip- Nevermind that, I didn't read what you wanted properly. You should include both point.h and screen.h from screen.cpp and point.cpp, providing full definitions. You can't create code actually [I]using[/I] a declared object unless it's defined. You should also move "extern Screen screen;" to screen.h, providing "screen" for both main.cpp and point.cpp.
[QUOTE=jA_cOp;15914882]-snip- Nevermind that, I didn't read what you wanted properly. You should include both point.h and screen.h from screen.cpp and point.cpp, providing full definitions. You can't create code actually [I]using[/I] a declared object unless it's defined. You should also move "extern Screen screen;" to screen.h, providing "screen" for both main.cpp and point.cpp.[/QUOTE] Thank you, trying that right now..... *Hopes* EDIT: So would putting extern Screen screen in Screen.h declare it at the same time or something? I always thought extern is used when it is externally declared somewhere else.
[QUOTE=DoctorSalt;15921635]Thank you, trying that right now..... *Hopes* EDIT: So would putting extern Screen screen in Screen.h declare it at the same time or something? I always thought extern is used when it is externally declared somewhere else.[/QUOTE] extern Screen screen; [I]declares[/I] screen, but it doesn't define it. It tells the linker to look for the definition possibly elsewhere, looking like this: [cpp]Screen screen;[/cpp] screen.cpp, main.cpp and point.cpp all get their own object files. If you want to share data between them, you need [I]declarations[/I] for that data, with the same name ("screen"). [cpp] extern int Hello; //declares Hello int Hello; //gives Hello linkage [/cpp]
Sorry, you need to Log In to post a reply to this thread.