NOTE: I made a DFH_lib.CPP where I included fstream and iomanip. I kept all the template functions in DFH_lib.CPP. Now, if I write the remaining NON-TEMPLATE functions in the MAIN.CPP and include DFH_lib.h only then it successfully runs. I don't understand why...
I was making a Data File Handling library using templates. I created two files:
DFH_lib.CPP
Lib_Test.CPP
I made a project and clicked on "Build All" under compile. I encountered the following linker error:
file_init(char near*) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP
AddColumn(const int near&) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP
file_init(char*);
and AddColumn(T data, const int& width);
and AddColumn(const int& width);
are functions which I only defined in DFH_lib.CPP. I only made calls to these functions in Lib_Test.CPP.
DFH_lib.CPP
template <class T> //Function belongs to Pretty Printing Libary
void AddColumn(T data, const int& width) {
cout<<setw(width)<<data<<" | ";
}
void AddColumn(const int& width) {
cout<<setw(width)<<setfill('_')<<"|";
}
void file_init(char* file) { //File initialization function
ofstream fout;
fout.open(file, ios::binary|ios::noreplace); //File Created, noreplace prevents data loss
fout.close();
}
Lib_Test.CPP
cout<<endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13);
file_init(file); //initializes the file
where "file" is defined as: char file[]="lib_Test.dat";
Could someone please explain why I'm getting this Linker Error? I don't understand what it means and therefore, how to fix it...
EDIT: I've noticed that this might be resulting due to a mistake done while including files, as I turned the Lib_Test.CPP into a "Hello World" program and the same error appeared. One more thing I noted: Only the non-template functions are causing the linking error!
DFH_lib.CPP
#ifndef _DFH_lib_cpp
#define _DFH_lib_cpp
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<string.h>
.....
#endif
Lib_Test.CPP
#include<iostream.h>
#include<conio.h>
#include"DFH_lib.CPP" //Including DFH Libary