-1

I am trying to make the constructor to set default values for template types as following:

With this struct template

template<class Type1, class Type2>
struct Pair

This is my constructor:

Pair(const Type1& t1 = Type1(), const Type2& t2 = Type2()) :
first(t1), second(t2)
{}

I use calls to the default constructor of each type. Everything goes with if I call the constructor with params:

Pair<float, double> pair_fd(1, 1);

But when I make the call without params the program gives linker errors.

Is possible to achieve what i am trying? If so, what am I missunderstanding?

Thank you all.

rain_
  • 734
  • 1
  • 13
  • 24
  • _"Is possible to achieve what i am trying?"_ Yes. _"If so, what am I missunderstanding?"_ There ar many ways this can fail, show a [MCVE] please. – πάντα ῥεῖ May 26 '16 at 15:42
  • 1
    [Cannot reproduce](http://coliru.stacked-crooked.com/a/936667f11b5b06a0). My link is an example of an MCVE. – chris May 26 '16 at 15:43
  • Why not just make a default constructor for `Pair` and use `std::enable_if_t::value>` (similarly for `Type2`)? Or just use `std::pair` instead of making a custom pair type. – sjrowlinson May 26 '16 at 15:45
  • @ArchbishopOfBanterbury, That's probably overly complicated for what the OP needs (the SFINAE, not the standard pair). I assume this is a learning exercise or so. – chris May 26 '16 at 15:47

1 Answers1

0

Solved.

I was trying to create the variable like

Pair<float, double> pair_fd();

Instead of

Pair<float, double> pair_fd;

Thanks @chris

rain_
  • 734
  • 1
  • 13
  • 24