Thread: What am I doing wrong? (Operator overloading)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    What am I doing wrong? (Operator overloading)

    Code:
    #ifndef TEST
    #define TEST
    using namespace std;
    
    template <typename T>
    class Test
    {
    private:
        T number;
    public:
    Test();
    Test(T);
    template <typename T> friend Test<T> operator+(Test<T> &a, Test<T> &b);
    template <typename T> friend ostream& operator<<(ostream& out, Test<T> &obj);
    };
    
    template <typename T>
    Test<T>::Test(T num1)
    {
        number = num1;
    }
    
    template <typename T>
    Test<T> operator+(Test<T> &a, Test<T> &b)
    {
        return Test<T>(a.number + b.number);
    }
    
    template <typename T>
    ostream& operator<<(ostream& out, Test<T> &obj1)
    {
        out<< obj1.number;
        return out;
    }
    
    #endif
    Code:
    #include <iostream>
    #include "Test.h"
    using namespace std;
    int main()
    {
        Test <int> obj1(5);
        Test <int> obj2(2);
        Test <int> obj3;
    
        obj3 = obj1 + obj2;
        cout << obj3;
        return 0;
    }
    I keep getting an error that says: "LNK2019: unresolved external symbol "public: __thiscall Test<int>::Test<int>(void)" (??0?$Test@H@@QAE@XZ) referenced in function _main"

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by UserName112 View Post
    Code:
    #ifndef TEST
    #define TEST
    using namespace std;
    
    template <typename T>
    class Test
    {
    private:
        T number;
    public:
    Test();
    Test(T);
    template <typename T> friend Test<T> operator+(Test<T> &a, Test<T> &b);
    template <typename T> friend ostream& operator<<(ostream& out, Test<T> &obj);
    };
    
    template <typename T>
    Test<T>::Test(T num1)
    {
        number = num1;
    }
    
    template <typename T>
    Test<T> operator+(Test<T> &a, Test<T> &b)
    {
        return Test<T>(a.number + b.number);
    }
    
    template <typename T>
    ostream& operator<<(ostream& out, Test<T> &obj1)
    {
        out<< obj1.number;
        return out;
    }
    
    #endif
    Code:
    #include <iostream>
    #include "Test.h"
    using namespace std;
    int main()
    {
        Test <int> obj1(5);
        Test <int> obj2(2);
        Test <int> obj3;
    
        obj3 = obj1 + obj2;
        cout << obj3;
        return 0;
    }
    I keep getting an error that says: "LNK2019: unresolved external symbol "public: __thiscall Test<int>::Test<int>(void)" (??0?$Test@H@@QAE@XZ) referenced in function _main"
    1) You've declared an empty constructor, so define it.
    2) The friend functions can't reuse the template parameter 'T' - name theirs somethings else.
    3) The copy constructor should take a constant reference to 'T'.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    Got it. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading the + operator
    By johnnyg in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2006, 02:25 AM
  2. Operator overloading
    By karb0noxyde in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2004, 10:23 AM
  3. Operator Overloading
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2003, 11:29 PM
  4. Overloading * operator
    By LouB in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2002, 04:35 PM
  5. What's wrong with operator overloading?
    By Flarelocke in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-19-2001, 03:31 PM