Most of the time, I "avoid" to have the following style in my single header file.
class a {
void fun();
};
void a::fun() {
}
In order to avoid the following error. I try to separate class definition in cpp file and class declaration in h file. For example, the below is the wrong example :
main.cpp
#include "b.h"
#include "a.h"
int main()
{
a aa;
b bb;
}
a.h
#ifndef A_H
#define A_H
#include <iostream>
class a {
public:
virtual int fun();
};
int a::fun()
{
int t;
std::cout << "a" << std::endl;
return t;
}
#endif
b.h
#ifndef B_H
#define B_H
#include <iostream>
#include "a.h"
class b {
public:
b();
};
#endif
b.cpp
#include "b.h"
#include "a.h"
b::b()
{
a aa;
aa.fun();
}
I will get the following error :
1>b.obj : error LNK2005: "public: virtual int __thiscall a::fun(void)" (?fun@a@@UAEHXZ) already defined in main.obj
However, when come to template, I will usually do it this way :
a.h
#ifndef A_H
#define A_H
#include <iostream>
template <typename T>
class a {
public:
virtual T fun();
};
template<typename T> T a<T>::fun()
{
T t;
std::cout << "a" << std::endl;
return t;
}
#endif
May I know it this a good practice?
Thanks.
Cheok