(Hopefully) a simple question on classes and inheritance.
-
I developed several class using unix g++ compiler. I developed one class in particular, called CEditString, that edits string pointers. Every other class that I created uses this CEditString class. At the top of all these other classes, I type #include "EditString.H" and within that class I type public: CEditString EditString; The problem is, when I compile, I get the following error: /code/EditString.H: In method `CEditString::CEditString()': In file included from /code/AtomLocation.H:11, from /code/ProteinStructure.H:13, from topView.C:7: /code/EditString.H:56: redefinition of `CEditString::CEditString()' /code/EditString.H:56: `CEditString::CEditString()' previously defined here. I know that I am redefining the class, but if I don't redefine, then I get that error that 'CEditString' undeclared. This is really frustrating. Is there any way of destroying the class so that when I do call it, I am not redefining it? Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
-
I developed several class using unix g++ compiler. I developed one class in particular, called CEditString, that edits string pointers. Every other class that I created uses this CEditString class. At the top of all these other classes, I type #include "EditString.H" and within that class I type public: CEditString EditString; The problem is, when I compile, I get the following error: /code/EditString.H: In method `CEditString::CEditString()': In file included from /code/AtomLocation.H:11, from /code/ProteinStructure.H:13, from topView.C:7: /code/EditString.H:56: redefinition of `CEditString::CEditString()' /code/EditString.H:56: `CEditString::CEditString()' previously defined here. I know that I am redefining the class, but if I don't redefine, then I get that error that 'CEditString' undeclared. This is really frustrating. Is there any way of destroying the class so that when I do call it, I am not redefining it? Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
Hello, Codeguru. We can use predefine class name in this case. /*#include "EditString.H"*/ class CEditString; and within that class I type public: CEditString EditString; HTH. -Masaaki Onishi-
-
I developed several class using unix g++ compiler. I developed one class in particular, called CEditString, that edits string pointers. Every other class that I created uses this CEditString class. At the top of all these other classes, I type #include "EditString.H" and within that class I type public: CEditString EditString; The problem is, when I compile, I get the following error: /code/EditString.H: In method `CEditString::CEditString()': In file included from /code/AtomLocation.H:11, from /code/ProteinStructure.H:13, from topView.C:7: /code/EditString.H:56: redefinition of `CEditString::CEditString()' /code/EditString.H:56: `CEditString::CEditString()' previously defined here. I know that I am redefining the class, but if I don't redefine, then I get that error that 'CEditString' undeclared. This is really frustrating. Is there any way of destroying the class so that when I do call it, I am not redefining it? Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
Is it possible that within one compile unit, you have #included the EditString.h file twice? Just hazarding a guess here? If that's the case, you should add the following two lines at the start of the EditString.h file; #ifndef EDITSTRING_H #define EDITSTRING_H ... ... ... rest of file and then at the very end of the file add this line #endif // for the EDITSTRING_H HTH.
-
I developed several class using unix g++ compiler. I developed one class in particular, called CEditString, that edits string pointers. Every other class that I created uses this CEditString class. At the top of all these other classes, I type #include "EditString.H" and within that class I type public: CEditString EditString; The problem is, when I compile, I get the following error: /code/EditString.H: In method `CEditString::CEditString()': In file included from /code/AtomLocation.H:11, from /code/ProteinStructure.H:13, from topView.C:7: /code/EditString.H:56: redefinition of `CEditString::CEditString()' /code/EditString.H:56: `CEditString::CEditString()' previously defined here. I know that I am redefining the class, but if I don't redefine, then I get that error that 'CEditString' undeclared. This is really frustrating. Is there any way of destroying the class so that when I do call it, I am not redefining it? Please, any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
Hi, If all the other classes use the one class, just #include that file in stdafx.h. That way it will be available to all the other classes. What you have here is called 'circular reference', where one header file gets included more than once. As the replies suggests, you can prevent this multiple inclusion by using #ifndef .... #define.....or use a 'forward declaration' in the header file by writing 'class CEditString'. Forward declarations, however, are only allowed for pointer members. That is, you can say CEditString* stPtr and not CEditString obj. What I usually do when I have a problem like this is draw a diagram showing what files have included what, and then I can figure out a way not to include a file more than once. If this doesn't help you then send me your whole project (syh@ufl.edu) and I'll be glad to look at it for you. I know how frustrating this can be. Good luck. sayed hashimi (another overworked graduate student).
-
Hello, Codeguru. We can use predefine class name in this case. /*#include "EditString.H"*/ class CEditString; and within that class I type public: CEditString EditString; HTH. -Masaaki Onishi-
That won't work. If you "forward declare" a class (eg, "class CEditString;") you may only use it for pointers and references until the full declaration is given.
-
That won't work. If you "forward declare" a class (eg, "class CEditString;") you may only use it for pointers and references until the full declaration is given.
Hello, James. I already noticed this with Mr. Hamishi post. But, simply just change CEditString string -> CEditString* string ,and '.' -> '->'. I think that this is more normal in C++ coding. Regards. -Masaaki Onishi-