I am new to templated classes. I am working on converting an original class to a templated class while doing so I get an error stating that I do not have an appropriate default constructor available. If I remember correctly a default constructor should be a constructor with no parameters. Any type of guidance to the right direction would be appreciated.
UPDATE REMOVED The parameterless constructor since its the same as the one with the DEFAULT_VALUE variables
Header file:
#include <iostream>
namespace cs_pairs
{
template <class T>
class OrderedPair
{
public:
//static const int DEFAULT_VALUE = int();
static const int DEFAULT_VALUE;
typedef std::size_t size_type;
typedef T value_type;
OrderedPair(value_type newFirst = DEFAULT_VALUE, value_type newSecond = DEFAULT_VALUE);
void setFirst(value_type newFirst);
void setSecond(value_type newSecond);
value_type getFirst() const;
value_type getSecond() const;
OrderedPair operator+(const OrderedPair& right) const;
bool operator<(const OrderedPair& right) const;
void print() const;
class DuplicateMemberError
{
};
private:
value_type first;
value_type second;
};
//template <class T>
//const int OrderedPair<T>::DEFAULT_VALUE = int();
}
Implementation File:
#include "orderedpair.h"
#include <iostream>
using namespace std;
namespace cs_pairs {
template <class T>
OrderedPair<T>::OrderedPair(value_type newFirst, value_type newSecond) {
setFirst(newFirst);
setSecond(newSecond);
}
template <class T>
void OrderedPair<T>::setFirst(value_type newFirst) {
// if statement to throw an exception if precondition not met goes here.
first = newFirst;
}
template <class T>
void OrderedPair<T>::setSecond(value_type newSecond) {
// if statement to throw an exception if precondition not met goes here.
second = newSecond;
}
template <class T>
typename OrderedPair<T>::value_type OrderedPair<T>::getFirst() const {
return first;
}
template <class T>
typename OrderedPair<T>::value_type OrderedPair<T>::getSecond() const {
return second;
}
template <class T>
OrderedPair<T> OrderedPair<T>::operator+(const OrderedPair<T>& right) const {
return OrderedPair(first + right.first, second + right.second);
}
template <class T>
bool OrderedPair<T>::operator<(const OrderedPair<T>& right) const {
return first + second < right.first + right.second;
}
template <class T>
void OrderedPair<T>::print() const {
cout << "(" << first << ", " << second << ")";
}
}
Client File Section where I first encounter the error:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "orderedpair.h"
using namespace std;
using namespace cs_pairs;
int main() {
int num1, num2;
OrderedPair<int> myList[10];
}