Call to Overloaded Function is Ambiguous?

Specifically on line 9.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//John 3
#include "MathVector.h"
#include <iostream>

int main()
{
   MathVector<double> mVec(20);
   MathVector<double> fVec(20);
   MathVector<int> iVec(20);

   cout << mVec[1] << endl;
   fVec = mVec;
   cout << fVec[1] << endl;
   for (int i = 0; i < 20; ++i)
   {
      mVec[i] = i + 1.2;
      cout << mVec[i] << " ";
   }
   cout << endl;

   fVec = mVec;
   for (int i = 0; i < 20; ++i)
   {
      fVec[i] = mVec[i] + 1;
      cout << fVec[i] << " ";
   }
   cout << endl;

   for (int i = 0; i < 20; ++i)
   {
      iVec[i] = i * 3;
      cout << iVec[i] << " ";
   } 
   
   return 0;
}


*************************
        Math.h
*************************
// Matthew 4
#ifndef MATHVECTOR_H
#define MATHVECTOR_H

#include <iostream>
#include <new> // new
#include <cstdlib> // exit function
using namespace std;

template <class T>
class MathVector
{
   private:
      T *mptr;
      int maxSize;
      void alloc(int s);
   public:
      MathVector() : MathVector(1)
      { }
      MathVector(int s)
      { alloc(s); }
      MathVector(const T &);
      ~MathVector();

      void insertArray(T *array, int size);


      T& operator[](const int &i);
      void memError();
      void subError();
      T operator+(const T &);
      T operator=(const T &);
};

// Finds enough memory for our vector.
template <class T>
void MathVector<T>::alloc(int s)
{
   if (s <= 0)
   {
      cout << "ERROR: Size too small.\n";
      exit(EXIT_FAILURE);
   }

   maxSize = s;
   
   try
   {
      mptr = new T[maxSize];   
   }
   catch(bad_alloc)
   {
      memError();
   }  
   
   T *const end = mptr + maxSize;
   for (T *ptr = mptr; ptr < end; ++ptr)
   {
      *ptr = 0;
   }
}

template <class T>
MathVector<T>::MathVector(const T &m)
{
   maxSize = m.maxSize;
   mptr = new T[maxSize];

   T *const end = mptr + maxSize;
   T *const rend = m.mptr + maxSize;
   for (T *ptr = mptr; ptr < end; ++ptr)
   {
      for (T *p2 = m.mptr; p2 < rend; ++ptr)
      {
         *ptr = *p2;
      }
   }
}

template<class T>
void MathVector<T>::memError()
{
   cout << "ERROR: Not enough memory.\n";
   exit(EXIT_FAILURE);
}

template<class T>
void MathVector<T>::subError()
{
   cout << "ERROR: Subscript out of range.\n";
   exit(EXIT_FAILURE);
}

template <class T>
T& MathVector<T>::operator[](const int &i)
{
   if (i < 0 || i >= maxSize)
      subError();
   return mptr[i];
}

template<class T>
T MathVector<T>::operator=(const T&right)
{
   if (this != &right)
   {
      maxSize = right.mSize;
      mptr = new T[maxSize];
      T *const end = mptr + maxSize;
      T *const rend = right.mptr + maxSize;
      for (T *ptr = mptr; ptr < end; ++ptr)
      {
         for (T *p2 = right.mptr; p2 < rend; ++p2)
         {
            *ptr = *p2;
         }
      }
   }
   return *this;
}

template<class T>
MathVector<T>::~MathVector()
{
   delete [] mptr;
   mptr = nullptr;
}

#endif 
Last edited on
You are including a custom header, "MathVector.h", yet you show a header with the name of "Math.h".

Is there another custom header you chose to not share with us, or did you misname your header? By the contents of the header it looks like a name mismatch.
The code you posted seems to give:

 In function 'int main()':
132:27: error: call of overloaded 'MathVector(int)' is ambiguous
132:27: note: candidates are:
63:1: note: MathVector<T>::MathVector(const T&) [with T = int]
19:7: note: MathVector<T>::MathVector(int) [with T = int]
10:7: note: constexpr MathVector<int>::MathVector(const MathVector<int>&)

In case T = int you have two too similar members:
1
2
MathVector<int>::MathVector(const int&) // from template
MathVector<int>::MathVector(int)


Rather than overload with MathVector(int) one can perhaps specialize what the MathVector(const T&) does.

However, if you do get past that, then you will stumble on the MathVector<double> mVec(20); because:
1
2
3
4
template <class T>
MathVector<T>::MathVector(const T &m)
{
   maxSize = m.maxSize; // what if m is double? 
Topic archived. No new replies allowed.