I have this code which is throwing conversion to non-scalar error here > TemplateTest t3 = t2;
But if I declare t3 and then assign t2 like this,
TemplateTest t3; t3 = t2;
then it works fine.
how can I get rid of the error?
Following is the complete code:
#include <iostream>
using namespace std;
template <typename T> class TemplateTest {
private:
T m_data;
public:
TemplateTest();
TemplateTest(const T inputData);
T GetData() const;
template<class U> TemplateTest<T>& operator= (const TemplateTest<U> &rhs);
void Print();
};
template <class T>
TemplateTest<T>::TemplateTest()
{
m_data = 0;
}
template <class T>
TemplateTest<T>::TemplateTest(const T inputData)
{
m_data = inputData;
}
template <class T>
void TemplateTest<T>::Print()
{
std::cout<<m_data<<std::endl;
}
template <class T>
T TemplateTest<T>::GetData() const
{
return m_data;
}
template<class T> template<class U>
TemplateTest<T>& TemplateTest<T>::operator= (const TemplateTest<U> &rhs)
{
m_data = rhs.GetData();
return *this;
}
int main()
{
TemplateTest<int> t1(12);
TemplateTest<double> t2(1.2);
TemplateTest<int>t3 = t2;
t3.Print();
return 0;
}