No Appropriate Default Constructor

Hi, I am working on converting a regular class into a templated class but I keep running into this error that says I do not have an appropriate default constructor and am a bit stuck. Implementation, header, and portion of client file will be provided

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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];

    srand(static_cast<unsigned>(time(0)));
    cout << "default value: ";
    myList[0].print();
    cout << endl;

    for (int i = 0; i < 10; i++) {
        myList[i].setFirst(rand() % 50);
        myList[i].setSecond(rand() % 50 + 50);
    }

    myList[2] = myList[0] + myList[1];

    if (myList[0] < myList[1]) {
        myList[0].print();
        cout << " is less than ";
        myList[1].print();
        cout << endl;
    }

    for (int i = 0; i < 10; i++) {
        myList[i].print();
        cout << endl;
    }

    cout << "Enter two numbers to use in an OrderedPair.  Make sure they are different numbers: ";
    cin >> num1 >> num2;
    OrderedPair x;

    x.setFirst(num1);
    x.setSecond(num2);

    {
        x.setFirst(num1);
        x.setSecond(num2);
    } catch (OrderedPair::DuplicateMemberError e) {
        x.setFirst(OrderedPair::DEFAULT_VALUE);
        x.setSecond(OrderedPair::DEFAULT_VALUE);
    }
    

    cout << "The resulting OrderedPair: ";
    x.print();
    cout << endl;
}
Last edited on
You defined a constructor with arguments so the "default" no argument constructor will not be automatically created by the compiler. So you will either need to create this no argument constructor yourself.

Topic archived. No new replies allowed.