1

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;
}
Renu
  • 29
  • 5

1 Answers1

1

Mostly some typos (no need to separate definition and declaration in template code). And a missing converting constructor (always make pairs of constuctors/assignments). And it is better to add an explicit cast too

#include <iostream>
//using namespace std; unlearn this

template <typename T> class TemplateTest 
{
private:
    T m_data;
public:

    TemplateTest()
    {
        m_data = 0;
    }

    TemplateTest(const T inputData)
    {
        m_data = inputData;
    }


    void Print()
    {
        std::cout << m_data << std::endl;
    }

    T GetData() const
    {
        return m_data;
    }


    template<typename U>
    TemplateTest(const TemplateTest<U>& rhs) :
        m_data{ static_cast<T>(rhs.GetData()) }
    {
        
    }

    template<typename U>
    auto operator=(const TemplateTest<U>& rhs)
    {
        m_data = static_cast<T>(rhs.GetData());
        return *this;
    }
};

int main()
{
    TemplateTest<int> t1(12);
    TemplateTest<double> t2(1.2);
    TemplateTest<int>t3 = t2;
    t3.Print();
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • thanks for the explanation. I haven't read the rules you mentioned in any tutorials/ articles so far. Is there any book/tutorial you would recommend please? – Renu Jun 07 '23 at 15:11
  • 1
    Absolutely : Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 07 '23 at 15:21