i want to use a class to print data stored as vector or array with different data types.
i also want the print function two take more than one vector or array or combination of both so that they can be written to file as two columns.
so i wrote the following class:
right now it has only one member function for printing two vectors. later i'll add additional functions as required.
note: there has to be template functions inside the class
i also want the object to be global so that i need not pass it as an argument to other calling functions
class printdata
{
public:
template<typename T1,typename T2>
void SaveData( vector<T1> &data1,vector<T2> &data2, std::string var)
{
std::ofstream myfile;
std::string filename;
std::stringstream a;
a << iter*dt;
filename = var + a.str();
filename += ".dat";
myfile.open(filename.c_str());
for (size_t i=0; i<data1.size(); i++)
{
myfile
<<std::left<<std::fixed//<<std::setw(8)<<i+1
<<std::left<<std::setw(12)<<std::setprecision(4)<<data1[i]
<<std::left<<std::setw(12)<<std::setprecision(8)<<data2[i]
<<std::endl;
}
myfile.close();
}
};
then i want to call this template function in another ordinary function written in a seperate cpp file
these function declarations are put in a header file. so i need know whether i should put the declaration of the template function in the header to use the function in different functions
i need know whether i should put the declaration of the template function in the header to use the function in different functions
yes, you would declare the class and the template function as usual, but you cannot define a template function in ur implementation (.cpp) file (though it's actually possible)
can anybody give the actual code that will do the job. i'm stuck here. the implementation in header file. the global object in the main.cpp and finally the actual method of using the template function in a another function file