state patterns, class type redefinition errors and other stuff

Started by
3 comments, last by Zahlman 19 years, 5 months ago
Hi, I did this tutorial (http://www.codeproject.com/cpp/statepattern3.asp) about state patterns today and after some minutes (hours) reading through the code and understanding it I tried to implement it into my own application. I ran into some problems though, and I hope someone here can help me even if the tutorial isn't from this site. You can find my sourcecode here: http://egy.nu/~u85snms/temp/test1.zip A build log can be found here: http://egy.nu/~u85snms/temp/BuildLog1.htm As you can see there are some "'class' type redefinition" errors (compiler). I googled and searched these forums and found some solutions, none worked for me though. Here (http://egy.nu/~u85snms/temp/test2.zip) you can find a test with the same code, but using "#pragma once" (which was one of the solutions I found) in all headerfiles. The build log for this you can find here: http://egy.nu/~u85snms/temp/BuildLog2.htm Even more errors (linker, which means adding #pragma once helps us getting through compiling?) here and if someone could help me with this I'd greatly appreciate it.
Advertisement
alright, heres what i came up with :
the reason for the redefinition errors is because you need to use extern <insert variable name> in your .cpp files.

here is a brief tutorial (its the simplest one that i could find) showing how extern should be used (its near the bottom of the page). also, #if defined guards would be nicer, instead of #pragma once (because the latter is compiler specific)

so you'd do something like
// globals .h#if !defined(GLOBALS_H)#define GLOBALS_Hint yeah = 1;// insert other vars here#endif// end of globals.h// test.cpp// global declaration#include <iostream>extern int yeah;int main(){   cout << yeah << endl;   return 0;}

hope that helped.
cheers.

<edit :: although you should be wary of passing global variables like that. it would probably be better to put them in a class, and have a global declaration of a class object. >
- stormrunner
Well it didn't help, not yet. I'm still getting the linker errors (test2.zip), might not be the exact same but there are alot of them. Thanks for the tips though. I will try to rework some code, especially to put the global variables in a "global class" instead, and come back tomorrow (read edit) if there's more problems. Feel free to add more thoughts until then =).
Just one more question for now:
I noticed when not including d3d9.h and d3dx9.h none of the state management-classes work (NULL is not defined for example), should I include these in every class .cpp-file or can I include it in my "global class"? So when I include the CShared-class header file (my global class) it includes d3d9.h and d3dx9.h through that. Or that is bad practice?

<edit>
OK I've already been trying some stuff, but I guess I'm a newbie and I would love to see an example of a class with shared (global) variables in it and how to use it. I don't know how to declare the pointers in the .cpp (the DirectX objects), and then how to use them (I might be doing it right in my try? source code at the bottom of the message).
Tips for that and also a general overlook of my code would be greatly appreciated, if someone got the time.. I guess there's other stuff that's also wrong. So if someone got nothing to do tonight, check this code out =):
http://egy.nu/~u85snms/temp/test3.zip

Here's the errors I get when trying to link it, when I include the shared class I've done in all my .cpp-files. http://egy.nu/~u85snms/temp/BuildLog3.htm

I'm going to continue to try but I feel kinda stuck right now =O
</edit>

[Edited by - rms on November 13, 2004 4:11:41 PM]
No one? I still haven't figured it out. Sorry for bumping, but the threads disappear from first page so quick. Someone might have missed it, hehe.

<edit>
I hope editing doesn't bump the thread. Anyway, I got it working now. By reading that article I understood what I should do with "extern" so I should thank both of you. Now I can continue with my project =)
</edit>

[Edited by - rms on November 15, 2004 5:35:06 AM]
The "toolbar" at the top of the page ought to be laid out like this:

[logout][control panel][bookmarks][who's online][active topics][stats]Forum FAQ[search][Kylotan's article on the whole source code file organization thing][Advocacy for Java, C#, Python, and basically every language other than C and C++, which are the only two which really have these problems, because of their compilation model]

This topic is closed to new replies.

Advertisement