This problem is coming again and again to me in different formats. I try to illustrate it with following simple example:
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
|
#include <iostream>
using namespace std;
class Vector
{
public:
void X()
{
cout<<"X in Vector\n";
}
};
class Matrix
{
public:
void X(int m)
{
cout<<"X in Matrix\n";
}
};
template <typename T>
int gg(T a, bool tt)
{
if(tt)
a.X();
else
a.X(21);
}
int main()
{
Vector v;
Matrix m;
gg(v, true);
gg(m, false);
}
|
It gives me following errors with g++:
test.cpp: In function ‘int gg(T, bool) [with T = Vector]’:
test.cpp:38: instantiated from here
test.cpp:31: error: no matching function for call to ‘Vector::X(int)’
test.cpp:8: note: candidates are: void Vector::X()
test.cpp: In function ‘int gg(T, bool) [with T = Matrix]’:
test.cpp:39: instantiated from here
test.cpp:29: error: no matching function for call to ‘Matrix::X()’
test.cpp:18: note: candidates are: void Matrix::X(int)
I understand the basic idea but on conditions it is giving error are never going to happen at runtime. How to suppress this?