Freiza Grandmaster Cheater
Reputation: 22
Joined: 28 Jun 2010 Posts: 662
|
Posted: Thu Feb 12, 2015 8:29 pm Post subject: Template Default parameter in Constructor [Solved] |
|
|
Code: | //template.h using MSVC++ 2010
#pragma once
#include <iostream>
using std::ostream;
template <typename T, typename U> class Pair {
private:
T first;
U second;
public:
// Pair() ;
Pair ( T x = T() , U y = U() ) ;
template<typename T, typename U>
friend ostream& operator<< ( ostream& thisPair, Pair<T, U>& otherPair );
};
template <typename T, typename U>
Pair<T, U>::Pair ( T x , U y ) : first ( T ( x ) ), second ( U ( y ) )
{cout << x << y;}
template <typename T, typename U>
ostream& operator<< ( ostream& os, Pair<T, U>& otherPair )
{
os << "First: " << otherPair.first
<< " "<< "Second: " << otherPair.second << endl;
return os;
}
//template.cpp
int main()
{
int a = 5, b = 6;
Pair<int,int> pair4();
Pair<int, int> pair1 ( a, b );
cout<<pair4;
cout<<pair1;
return 0;
} |
How to make a constructor or a member function to take default value? The code above is giving linker error for pair4 when using cout statement. The code works perfectly when cout<<pair4(); is commented. I am trying to mimic a constructor taking 0,1 or 2 argument using a single default constructor in a template class.
[Edit]
use Pair<int,int> pair4; instead of pair4();
Thank You
_________________
|
|