help on using template function inside a class in a seperate function

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
Why do these function templates even need to be a member of some empty singleton class? That's the job for a namespace.

And yes, the complete definition (not just the declaration) of a template needs to be in the header file.
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)

Is that what you're asking ?
Last edited on
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
You mean like :
X.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef X_H
#define X_H

class X
{
    template <typename T>
    void f ( const T& t )
    {
        // ...
    }
};

#endif // X_H 


main.cpp
1
2
3
4
5
6
7
8
#include "X.h"

X g_x1; // global object ( pls consider using other approach )

int main()
{
    g_x1.f<int>( 3 ); // deduction will also work
}


other.cpp
1
2
3
#include "X.h"

// ... you have to instantiate another X to be able to use the method or use extern ? (i can't remember) 


BTW, I suggest using namespace or even plain global function for ur function instead of putting it in an empty class
xxx.h
1
2
3
4
5
6
7
8
// header guard
#include 's

template <typename T1, typename T2>
void SaveData( std::vector<T1> &data1, std::vector<T2> &data2, std::string var)
{
    // definition of template function
}


then you can simply call the function by including the appropiate file :
main.cc
1
2
3
4
5
6
7
8
#include "xxx.h"

int main()
{
    // ...
    SaveData( myVec, otherVec, aString );
    // ...
}
Last edited on
Topic archived. No new replies allowed.