E
E. Robert Tisdale
cat X.h
#ifndef GUARD_X_H
#define GUARD_X_H 1
#include <iostream>
template<class T>
class X {
private:
// representation
T D;
public:
// functions
T data(void) const { return D; }
// constructors
X(T d = 0): D(d) { }
X(const X& x);
template<class S>
X(const X<S>& x);
friend
std:stream& operator<<(
std:stream& os, const X<T>& x) {
return os << x.data();
}
};
#endif//GUARD_X_H 1
template<class T>
X<T>::X(const X& x): D(x.D) {
std::cerr << "copy constructor" << std::endl;
}
template<class T>
template<class S>
X<T>::X(const X<S>& x): D(x.data()) {
std::cerr << "conversion constructor" << std::endl;
}
template X<float>::X(const X<int>& x);
for `X<float>::X(const X<float>&)'
X.cc:4: error: candidates are: \
X<T>::X(const X<T>&) [with T = float]
X.h:17: error: \
template<class S> X::X(const X<S>&) [with S = S, T = float]
What did I do wrong?
#ifndef GUARD_X_H
#define GUARD_X_H 1
#include <iostream>
template<class T>
class X {
private:
// representation
T D;
public:
// functions
T data(void) const { return D; }
// constructors
X(T d = 0): D(d) { }
X(const X& x);
template<class S>
X(const X<S>& x);
friend
std:stream& operator<<(
std:stream& os, const X<T>& x) {
return os << x.data();
}
};
#endif//GUARD_X_H 1
#include "X.h"cat X.cc
template<class T>
X<T>::X(const X& x): D(x.D) {
std::cerr << "copy constructor" << std::endl;
}
template<class T>
template<class S>
X<T>::X(const X<S>& x): D(x.data()) {
std::cerr << "conversion constructor" << std::endl;
}
template X<float>::X(const X<int>& x);
X.cc:15: error: ambiguous template specialization `X<>' \template X said:g++ -Wall -ansi -pedantic -c X.cc
for `X<float>::X(const X<float>&)'
X.cc:4: error: candidates are: \
X<T>::X(const X<T>&) [with T = float]
X.h:17: error: \
template<class S> X::X(const X<S>&) [with S = S, T = float]
What did I do wrong?