Reputation: 9183
I have three files.
Wood.cpp
Brick.cpp
Wall.cpp
And my main()
function is in Brick.cpp:
Now, when I run the project, it throws error that the className
(which is in Wall.cpp) is undefined. What should I do?
I think the main()
function is run before the delaration of Wall.cpp file.
Upvotes: 0
Views: 25
Reputation: 4432
You need a way to tell the c++ compiler what function would be available and implemented, in C++ the way is using headers files:
Example:
File a.hpp
class A {
// variable members
// function signatures or inline functions
};
File a.cpp
// Implementation of functions in class A and initialization of static variables in A
File b.hpp
#include "a.hpp"
// Could use class A
class B {
A m_a_member_variable;
}
File b.cpp
// Implementation of functions in class B and initialization of static variables in B that could use classes and declarations in a.hpp (ex: class A)
Upvotes: 1
Reputation: 5334
I think the main() function is run before the delaration of Wall.cpp file.
there is no run before while compiling. also, the compilation order is defined by the compiler.
you need to include the header (ussualy *.h, *.hpp, *.hh) file (if you have one, otherwise you have to create one) where className
is defined into the source file where you use this class.
for the compiler it is enough to know the declaration (which is in the header) of a class to compile the source. the declaration is then necessary for the linker.
Upvotes: 0