0

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?

  • Why are you including source files?!! Like you have in your given example `#include "dir3.cpp"` and `#include "vec3.cpp"`. You can refer to a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the basics of templates and how to include/use them. – Jason Jun 10 '22 at 15:21
  • How should the files know each other without including them? BTW: Putting templated functions in cpp files will never work as long you did not manually instantiate them. Feels you should learn what is a translation unit, how to use templates and organizing a project into files... – Klaus Jun 10 '22 at 15:23
  • 1
    Essential reading: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Nathan Pierson Jun 10 '22 at 15:24
  • I saw this solution on codeproject.com if I don't include the .cpp files and give them directly to g++ the vec3.cpp or the dir3.cpp never gets compiled because the instantiations take place somewhere else and the linker will give an error. @anoop-rana – web_.junkie Jun 10 '22 at 15:25
  • @web_.junkie Yes, i know that site from where this solution that you're using comes from. I just have one thing to say about that which is that their given solution(s) are wrong. Don't include source files in your program as a best practice and also the solution that that particular site gives don't actually work and even if they work they result in undefined behavior. Refer to the duplicate that i linked to your question instead. – Jason Jun 10 '22 at 15:27
  • @Klaus i thought if i include eveything with the correct order in main.cpp it should be understandable. i even used g++ -Wall -C -E to check that they get included in the correct order. – web_.junkie Jun 10 '22 at 15:28
  • @web_.junkie As a rule of thumb(or best practice) don't include source files. – Jason Jun 10 '22 at 15:33
  • I read that question before posting. as a matter of fact, the marked answer on that post has a solution almost identical to this(they used typename instead of class) that's what got me confused to ask this here. @anoop-rana – web_.junkie Jun 10 '22 at 15:34

0 Answers0