I'm trying to write a piece of code in c++ and to make things tidy I decided to split the source into files. So basically each header file contains the declarations for the class and its fields and methods and at the bottom, it includes a .cpp file with all the implementations. I had to do the quite bizarre act of including the source after the header because I'm using template classes and I had to do this to fix the linker error.
Anyway, the code looks something like this:
common/vec3.h:
#ifndef
#define protector blah blah
template<class T>
class vec3{
//
// fields and methods
// int bruh
// double func(vec3<T>);
//
};
#include "vec3.cpp"
#endif
in common/vec3.cpp i only implement the functions declared in vec3.h:
//=========vec3============
template <class T>
double vec3<T>::func(vec3<T> v){
//...
}
//...
common/dir3.h:
#ifndef
#define protector blah blah
template<class T>
class dir3{
//
//
//
void operator=(vec3<T>); // this line is causing error
//
//
};
#include "dir3.cpp"
#endif
common/dir3.cpp:
//======dir3===========
//
//
//
template<class T>
void dir3<T>::operator=(vec3<T> v){
// ...
}
//
//
in my main file i include these libraries like:
#include <iostream>
#include <list>
// ...
using namespace std;
#include "common/vec3.h"
#include "common/dir3.h"
// ...
int main(){
//...
}
but when I try to compile this project with
g++ main.cpp -o main
I get the error:
In file included from main.cpp:8:0:
common/dir3.h:14:17: error: 'vec3' is not a type
void operator=(vec3<T>);
^
even though I included dir3.h
after vec3.h
and they both have the template class definitions.
I know the nuances of the differences between classes and template classes to some extent but i can't see any other way to use a template class within another one(it doesnt need instantiating).
I even tried including the vec3.h
file in the dir3.h
file but it didn't work.
why isn't this working and what should I do?