In order to use another class and it's methods you need to know WHAT to use and HOW to do it:
- it's interface ( what you can use ... )
- implementation of each method ( ... and how )
You know the interface of the class by including it's header file. Knowing the implementation is handled by the compiler - you can compile the class with all your project sources, or link against separate library - note that you don't need to include .cpp (implementation) files of the class you want to use.
Suppose you have a class
// test.h
class Test{
public:
void method();
};
// test.cpp
void Test::method(){
}
// test.h
class Test{
public:
void method();
};
// test.cpp
void Test::method(){
}
To copy to clipboard, switch view to plain text mode
In order to use it in your sources you need to:
a) include the class definition
//other_file.cpp
#include "test.h"
//...
Test t;
t.method();
//other_file.cpp
#include "test.h"
//...
Test t;
t.method();
To copy to clipboard, switch view to plain text mode
b) compile the class implementation files (test.cpp) along with your project OR add it to your project as separate library, compiled earlier
Both a) and b) are mandatory, if you don't have a) - then errors like "Test was not declared" appears, if you don't have b) - you may see something like "undefined reference to Test::method()"
Hope it was clear enough.
Bookmarks