How my program is find these two classes?

Hello I have the following class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef __MATRIX4x4_h
#define __MATRIX4x4_h

//#include "Vec4f.h"

//class Vec4f;

class Matrix4x4 {
   public:
      inline Matrix4x4 ();
      inline void setIdentity ();
      inline void setTranslation (const Vec3f& translation_values);
      inline void setRotation (const Vec3f& rotation_values);
      inline void lookAt (const Vec3f& camera_origin, const Vec3f& camera_target);
      int a;
   private:
      Vec4f row1, row2, row3, row4;
};

inline Matrix4x4::Matrix4x4 () {
   row1 = Vec4f (0.0f,0.0f,0.0f,0.0f);
   row2 = Vec4f (0.0f,0.0f,0.0f,0.0f);
   row3 = Vec4f (0.0f,0.0f,0.0f,0.0f);
   row4 = Vec4f (0.0f,0.0f,0.0f,0.0f);
   a=5;
}

inline void Matrix4x4::setIdentity () {
   row1 = Vec4f (1.0f,0.0f,0.0f,0.0f);
   row2 = Vec4f (0.0f,1.0f,0.0f,0.0f);
   row3 = Vec4f (0.0f,0.0f,1.0f,0.0f);
   row4 = Vec4f (0.0f,0.0f,0.0f,1.0f);
}

inline void Matrix4x4::setTranslation (const Vec3f& translation_values) {
   row1 = Vec4f (1.0f,0.0f,0.0f,translation_values.getX());
   row2 = Vec4f (0.0f,1.0f,0.0f,translation_values.getY());
   row3 = Vec4f (0.0f,0.0f,1.0f,translation_values.getZ());
   row4 = Vec4f (0.0f,0.0f,0.0f,1.0f);
}


as can be seen, to do its stuff, this class Matrix4x4 uses Vec3f and Vec4f instances. However, as can be seen in the beginning of my code, I commented out both the import for Vec4f and its foward declaration. Without these, it shall not have access to the information required to build the Vec4f instances inside the above methods. Neither it shall be able to deal with Vec3f info, since there is neither a import or a foward declaration for it (where before it should get the info from the Vec4f import, as it's a derived class)

yet, despite all of that, the program compiles and run!

What's going on here?
Last edited on
This is how stuff works with MSVC compiler, but not in all cases.

it's very likely that your source file includes "Vec4f.h" before "Matrix4x4.h".

note that compiler doesn't compile headers, it includes them into source files and then compiles the sources.

if your headers don't include anything then the order of inclusions in source file is important, otherwise if your headers do include what they need then the order of inclusions in source files is not important.

also it's possible that you precompiled header, in which case compiler doesn't care what you do in headers at all becase precompiled header will likely include all that is needed if you configure it that way.

If you would like to investigate what your preprocessor forwards to compiler then a good option is to preprocess the file in question. see /P switch to preprocess a file.
https://docs.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=vs-2019
Last edited on
Actually, I am using g++ through CodeBlocks. There is a file called Ogl.depend that lists the files and directories that every file depends on, but nothing shows in the file Matrix4x4.h
Last edited on
1
2
3
4
5
6
//a.h
#ifndef a_guard
#define a_guard
void a()
{}
#endif 

1
2
3
4
5
6
7
8
//b.h
#ifndef b_guard
#define b_guard
void b()
{
  a();
}
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//main.cpp

//change the order and the error will occur.
//this is what malibor was trying to tell you
//include files just "copy and paste" code, 
//the scope behind the header can affect it.
//this is also why you always need include guards.
#include "a.h"
#include "b.h"
int main(/*yadda*/)
{
  a();
  return 0;
}
Topic archived. No new replies allowed.