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. Referencing a class in other in a other class

Referencing a class in other in a other class

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 6 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi When have the following code class a { int a; LPCTSTR b; c d; } where "c" is defined class c { … } so if class a is in a.h and class c is in c.h what is the difference between #include "c.h' in a.h or declaring class c; or as a better question what is the way to resolve the unresolved reference in a.h for the type "c" which is a class is it to "include "c.h" or declare class c; that c is of type class and I guess in that case "c" will get resolved by the linker Hope this question makes sense thanks

    V J B 3 Replies Last reply
    0
    • F ForNow

      Hi When have the following code class a { int a; LPCTSTR b; c d; } where "c" is defined class c { … } so if class a is in a.h and class c is in c.h what is the difference between #include "c.h' in a.h or declaring class c; or as a better question what is the way to resolve the unresolved reference in a.h for the type "c" which is a class is it to "include "c.h" or declare class c; that c is of type class and I guess in that case "c" will get resolved by the linker Hope this question makes sense thanks

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Did you try to add the forward declaration

      class c;

      in the a.h? Does it compile? If Yes - then use it. Otherwise put

      #include "c.h"

      in a.h

      F 1 Reply Last reply
      0
      • V Victor Nijegorodov

        Did you try to add the forward declaration

        class c;

        in the a.h? Does it compile? If Yes - then use it. Otherwise put

        #include "c.h"

        in a.h

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks

        L S 2 Replies Last reply
        0
        • F ForNow

          Hi When have the following code class a { int a; LPCTSTR b; c d; } where "c" is defined class c { … } so if class a is in a.h and class c is in c.h what is the difference between #include "c.h' in a.h or declaring class c; or as a better question what is the way to resolve the unresolved reference in a.h for the type "c" which is a class is it to "include "c.h" or declare class c; that c is of type class and I guess in that case "c" will get resolved by the linker Hope this question makes sense thanks

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          In this case, a.h needs to include c.h since the former needs to know the full definition of c.

          1 Reply Last reply
          0
          • F ForNow

            the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            No, the linker connects object code, it has no knowledge of classes. Classes are purely a mechanism used in source languages and converted into object code by the compiler.

            1 Reply Last reply
            0
            • F ForNow

              the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              In the code you posted, the forward declaration can not work. If it worked in your code, it may be including c.h without your knowing, maybe indirectly through another header. If #include doesn't work, read the error message. The only reasons for #include not working that I can think of is that either the code in your c.h has a syntax error, or you forgot to add an Include guard[^], causing a duplicate definition.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              1 Reply Last reply
              0
              • F ForNow

                Hi When have the following code class a { int a; LPCTSTR b; c d; } where "c" is defined class c { … } so if class a is in a.h and class c is in c.h what is the difference between #include "c.h' in a.h or declaring class c; or as a better question what is the way to resolve the unresolved reference in a.h for the type "c" which is a class is it to "include "c.h" or declare class c; that c is of type class and I guess in that case "c" will get resolved by the linker Hope this question makes sense thanks

                B Offline
                B Offline
                Bram van Kampen
                wrote on last edited by
                #7

                Well, Well, Well! This question is the result of too much abstraction in education of computer scientists! A bit of learning of how a compiler works, Things like Bits and Bytes, the underlying mechanisms, and what it cannot do would help you here. This is an insolvable problem! Ultimately, a compiler lays out code, reserves blocks of memory of a certain size, etc. The compiler needs to know how much memory is needed for each user defined type (in C(++)) that is a union, structure or a class! When you Declare a class, you tell the compiler to take note of the name of the class! The Linker may than be able to find that class properly defined in another file, and hence in another .obj file! The compiler cannot at that stage know anything about the size of the object, so, all you can use is either a pointer of an object of that type, or, a reference to it! A Reference to an object is very similar to a pointer, it is a compiler guaranteed pointer, that cannot be null, and always points at an object of the type! In Your code class 'c' needs to be defined in the file before class a in which it is used! Otherwise your compiler cannot calculate the size of class a! Best of luck learning more, There may be rules around this conundrum in synthetic languages such as C#,Java etc. I would know nothing of these! There are no such shortcuts in C or CPP! I encourage anyone to take up C or CPP! It are the base languages on which all others are built! A knowledge of Machine Code, and how it translates in ASM, would also help you

                Bram van Kampen

                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