1

I've been searching for a few days already and can't seem to find a proper answer to my question.

In my program, I need to have a first class (D) that reads a file and instanciate some basic objects, that will be used to create some others. Once they're created, I call a parsing function with a collection of objects PC, which are NECESSARILY of class or child of class P.

To do so, I've implemented a collection of PC, that is merely an encapsulation of a vector storing PC objects.


In D.h

class D {
    /*...*/
    template<typename PC>
    parse(vector<string> cmdline, V<PC> vpcs);
}

In D.cpp

template<typename PC>
parse(vector<string> cmdline, V<PC> vpcs) {
     /* parse options */
     /* check with a functor if PC is really of class (child of) P */
     vpcs.push_back( PC(args) );
}

In V.h

template<typename PC>
class V {
    /*...*/
    vector<PC> vpcs;
}  

In main.cpp

{
    D des;
    vector<string> cmdline;
    V<P*> vps;
    des.parse(cmdline, vps);
}

Why do I want to do this? Because I'm writing a library and we have to let the future developers create their own child of P for specialized programs so I want to do something really generic (therefore : templated :)). I've already implemented and tested my function with class P, fully working, now I want to improve this. But I don't manage to compile my code or neither understand what I've been doing wrong.

(Also, for the record : first time using templates so I've read a lot of documentation but application is always a bit difficult!)

Thanks for your future answers and let me know if I can add more details!

EDIT : Okay, so the post given as "duplicate" (which I never read during my research, maybe not the correct keywords I typed in?) helped me for one thing but in fact it was not this problem at all. I had another one that I manage to resolve. In fact, I intended to have a collection of pointers, so instead of passing as PC the class*, I decided to pass only the class and just create the smart pointer inside a method of my templated "vector" class. Now it works, thank you all :)

Holt
  • 36,600
  • 7
  • 92
  • 139
Holywa
  • 51
  • 1
  • 1
  • 10
  • 1
    What doesn't work? What compiler error do you get? – TartanLlama Jan 07 '16 at 10:41
  • 4
    The code in `D.cpp` should be in `D.h` actually. See [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – πάντα ῥεῖ Jan 07 '16 at 10:41
  • @πάνταῥεῖ : Why? I assumed that the implementation of my function could be in my cpp? Or is it specific to templates? EDIT : Thanks for the link, will read it right now! – Holywa Jan 07 '16 at 10:46
  • @TartanLlama : The compiler says that me V is not a template. I've already tested some simple things with a coworker yesterday, it even didn't manage to recognize and link the method in the cpp and the header – Holywa Jan 07 '16 at 10:46
  • `template` in `V.h` should be `template `. – TartanLlama Jan 07 '16 at 10:47
  • @Holywa Read in the dupe It's specific for template code, yes. – πάντα ῥεῖ Jan 07 '16 at 10:47
  • @TartanLlama ( In fact, it already is, I've just write it wrong when "simplified" it :p ) – Holywa Jan 07 '16 at 10:49
  • Please don't add `[SOLVED]` to your title, the `[duplicate]` is sufficient to indicate that this question has an answer. – Holt Jan 07 '16 at 13:15
  • But I edited to say that this one not my real problem in fact? – Holywa Jan 07 '16 at 13:33

0 Answers0