0

I would like to create my first template class for files manipulation. To be more specific I started with.

template <typename T>
class Foo
{
public:
Foo();
~Foo();

std::vector<Foo<T>> vec;

void parsing(T&v,T&filename);


};

template<typename T>
inline Foo<T>::Foo()
{
}

template<typename T>
inline Foo<T>::~Foo()
{
}

template<typename T>
inline void Foo<T>::parsing(T&v, T&filename){

std::fstream file;
file.open(filename, std::ios_base::in)
if (plik.good()){
    typename T::value_type tmp;
    while (!plik.eof()){
        plik >> tmp;
        v.push_back(tmp);
    }
    file.close();
    }
}

And then I wanted to do those kind of initialization.

Foo<std::string> file;
file.parsing(vec,"file.txt");

I'm kinda new with templates so I've got following questions.

  1. How to read/save generic types of files ? My plan was to read a file, counting words, or numbers, and pushing this into Foo-type vector. Then after manipulation on an vector I wanted to save a results of counting.

  2. How about constructors and intitialization of variables and vectors ? Is there any specific rules that I should follow with in template class ?

Sorry for this chaotic style of question but I dont't know how to start. Thanks for all responses.

Return.h
  • 129
  • 1
  • 2
  • 6
  • Mandatory read: [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Dec 13 '17 at 08:36

1 Answers1

0

I think you are close. The example how you want to use your module looks good, it just doesn't match the function prototype.
Change your parsing() function like this:

void Foo<T>::parsing(std::vector<T>& v, const std::string& filename);

Regarding your questions:

  1. Your solution is one type to do it. Just the vector should not be a vector of Foo's, but of T's.
  2. Initialisation of variables is not different in a template than that in a normal class.
Rene
  • 2,466
  • 1
  • 12
  • 18
  • okay thanks for advice. it really helped me. I've changed a little bit my this code but your declaration is great. Now I have another kind of problem. When I want to read type I have to use some buffer and reading this character by character with file.get(buffer). If I want to read type I have to use same construction but with getline. And my question. How to make specialization of template for approprietly type ? I read about this a little bit but my ways of doing it was incorrect. By the way I'm reallly grateful for support – Return.h Dec 15 '17 at 17:47
  • What you are looking for is a partial template specialisation: `template<> class Foo ...`. There you can implement the special handling. But you have to implement all methods from the template Foo<>, not just the ones that are special for std::string. – Rene Dec 15 '17 at 18:02
  • And: If my answer helped you, why not upvote it? :-) – Rene Dec 15 '17 at 18:03
  • I upvoted but I don't have any rank so thats why It's not visible :X – Return.h Dec 15 '17 at 18:31