K
Kenneth Massey
I have run into a peculiar problem, in which the following sample code
does not compile (gcc 3.3). I have a template class with a member
function that should take a const version of the template parameter.
However, when I try to use the class with (T=int*), the compiler seems
to ignore the const and gives an error if I pass a const int*. Any
help would be much appreciated.
Kenneth
// test.cpp: In function `int main()':
// test.cpp: error: invalid conversion from `const int*' to
`int*'
// test.cpp: error: initializing argument 1 of `int
myclass<T>::check(T)
#include <stdio.h>
template<class T>
class myclass {
T x;
public:
myclass(T x0):x(x0) {}
int check(const T y) { return x==y; }
// compiles fine with this line: T is explicitly replaced by int*
// int check(const int* y) { return x==y; }
};
int main() {
int k;
myclass<int*> mc(&k); // T = int*
const int* p = &k;
printf("%d\n",mc.check(p)); // calling check() causes the error
return 0;
}
does not compile (gcc 3.3). I have a template class with a member
function that should take a const version of the template parameter.
However, when I try to use the class with (T=int*), the compiler seems
to ignore the const and gives an error if I pass a const int*. Any
help would be much appreciated.
Kenneth
// test.cpp: In function `int main()':
// test.cpp: error: invalid conversion from `const int*' to
`int*'
// test.cpp: error: initializing argument 1 of `int
myclass<T>::check(T)
#include <stdio.h>
template<class T>
class myclass {
T x;
public:
myclass(T x0):x(x0) {}
int check(const T y) { return x==y; }
// compiles fine with this line: T is explicitly replaced by int*
// int check(const int* y) { return x==y; }
};
int main() {
int k;
myclass<int*> mc(&k); // T = int*
const int* p = &k;
printf("%d\n",mc.check(p)); // calling check() causes the error
return 0;
}