I'm trying to instantiate a template class using an instantiation of the same template class as the type. Previous answers using an instantiation of a different template class suggest that this code should work:
#include <stdio.h>
#include <typeinfo>
template <class T> class pair {
private:
T first;
T second;
public:
pair(T x, T y) {
first = x;
second = y;
}
void printit() {
if (typeid(int) == typeid(T)) {
printf("pair(%i,%i)\n", first, second);
} else {
printf("type not supported yet\n");
}
}
};
int main(int argc, char ** argv) {
printf("simplertest\n");
int one = 1;
int two = 2;
pair<int> pi(one,two);
pair<pair<int> > pp(pi, pi); // should make pair whose members are pairs
}
but I get an error from g++
> g++ simplertest.cpp
simplertest.cpp: In instantiation of ‘pair<T>::pair(T, T) [with T = pair<int>]’:
simplertest.cpp:27:31: required from here
simplertest.cpp:9:20: error: no matching function for call to ‘pair<int>::pair()’
pair(T x, T y) {
^
simplertest.cpp:9:5: note: candidate: pair<T>::pair(T, T) [with T = int]
pair(T x, T y) {
^~~~
simplertest.cpp:9:5: note: candidate expects 2 arguments, 0 provided
simplertest.cpp:4:26: note: candidate: constexpr pair<int>::pair(const pair<int>&)
template <class T> class pair {
^~~~
simplertest.cpp:4:26: note: candidate expects 1 argument, 0 provided
simplertest.cpp:4:26: note: candidate: constexpr pair<int>::pair(pair<int>&&)
simplertest.cpp:4:26: note: candidate expects 1 argument, 0 provided
simplertest.cpp:9:20: error: no matching function for call to ‘pair<int>::pair()’
pair(T x, T y) {
^
simplertest.cpp:9:5: note: candidate: pair<T>::pair(T, T) [with T = int]
pair(T x, T y) {
^~~~
simplertest.cpp:9:5: note: candidate expects 2 arguments, 0 provided
simplertest.cpp:4:26: note: candidate: constexpr pair<int>::pair(const pair<int>&)
template <class T> class pair {
^~~~
simplertest.cpp:4:26: note: candidate expects 1 argument, 0 provided
simplertest.cpp:4:26: note: candidate: constexpr pair<int>::pair(pair<int>&&)
simplertest.cpp:4:26: note: candidate expects 1 argument, 0 provided
> g++ --version
g++ (SUSE Linux) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
What am I doing wrong here?