Referencing a class in other in a other class
-
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
-
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
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
-
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
-
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
In this case, a.h needs to include c.h since the former needs to know the full definition of c.
-
the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks
-
the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks
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)
-
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
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