4 votes

How to use the templates of a File in another File in c ++?

I have a file with extension A.cpp which has templates that I would like to use in the B.cpp file. I tried to put the following in B.cpp :

 #include "A.cpp"
 

But I get the following error:

 C:\Users\friky\Documents\Octavo Semestre\Inteligencia Artificial\Arbol.cpp  In function 'int main(int, char**)':
3   5   C:\Users\friky\Documents\Octavo Semestre\Inteligencia Artificial\Arbol.cpp  [Error] redefinition of 'int main(int, char**)'
1   0   C:\Users\friky\Documents\Octavo Semestre\Inteligencia Artificial\Arbol.cpp  In file included from C:\Users\friky\Documents\Octavo Semestre\Inteligencia Artificial\Arbol.cpp
286 5   C:\Users\friky\Documents\Octavo Semestre\Inteligencia Artificial\arbolABB-templ.cpp [Error] 'int main(int, char**)' previously defined here
 

3voto

Miguelw Points 106

I think it's because when you include the file "A.cpp" there is an ambiguity between the "main" functions of each one.

I recommend you put what you want to use in those two files in a file "Templates.h" and import it in both.

I hope it helps.

A greeting.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X