Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (Hopefully) a simple question on classes and inheritance.

(Hopefully) a simple question on classes and inheritance.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionoop
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erich Ruth
    wrote on last edited by
    #1

    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)

    M C U 3 Replies Last reply
    0
    • E Erich Ruth

      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)

      M Offline
      M Offline
      Masaaki Onishi
      wrote on last edited by
      #2

      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-

      J 1 Reply Last reply
      0
      • E Erich Ruth

        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)

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • E Erich Ruth

          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)

          U Offline
          U Offline
          User 4315
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          • M Masaaki Onishi

            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-

            J Offline
            J Offline
            James Curran
            wrote on last edited by
            #5

            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.

            M 1 Reply Last reply
            0
            • J James Curran

              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.

              M Offline
              M Offline
              Masaaki Onishi
              wrote on last edited by
              #6

              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-

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups