Overload + operator with templates

Hey, I get LNK2019 linker error: unresolved external character when trying to use the overloaded + operator. I will show you snip it from the class and how I use it mainly. If you need to know more, let me know, I'll just try to keep it short.

/** vec.h **/
#ifndef __VEC_H_
#define __VEC_H_

#include <iostream>
#include <vector>

namespace xoor{

    template<typename T> 
    class vec{

    public:
        inline friend vec<T> operator + (const vec<T>&, const vec<T>&);
        inline const vec<T>& operator += (const vec<T>&);

    private:
        std::vector<T> m_index;
    }; // Vec.


    template<typename T> 
    vec<T>& operator + (const vec<T>& a, const vec<T>& b){
        vec<T> product = a;
        product += b;

        return product;
    } // Addition.

    template<typename T> 
    const vec<T>& vec<T>::operator += (const vec<T>& v){
        for (unsigned short i =0; i < m_index.size(); ++i){
            if (i >= v.size())
                break;
            m_index[i] += v.getIndex()[i];
        }

        return * this;
    } // Addition Compound.


} // xoor

#endif // __VEC_H_

Please note that I also have overload, so I just get access to parts of m_index. getIndex () just returns m_index. And size () returns m_index.size ()

/** main.cpp **/

#include <iostream>
#include "vec.h"

void testHook();

int main(){
    testHook();
    system("PAUSE");
    return 0;
}

void testHook(){
    using namespace xoor;
    vec<double> vA(3); // passing 3 for 3 elements
    vec<double> vB(3);

    // v + v
    std::cout << "\n\tA + B = ";
    vec<double> vAB(3);
    vAB = vA + vB; // PRODUCES THE LNK2019
    vAB.print(std::cout); // Outputs the vec class to the console.
}

Error message:

Error   1   error LNK2019: unresolved external symbol "class xoor::vec<double> __cdecl xoor::operator+(class xoor::vec<double> const &,class xoor::vec<double> const &)" (??Hxoor@@YA?AV?$vec@N@0@ABV10@0@Z) referenced in function "void __cdecl testHook(void)" (?testHook@@YAXXZ)    main.obj

Update:

The following is directly above the class definition. I keep getting the same linker error as described above.

    template<typename T> 
    class vec;

    template<typename T> 
    vec<T> operator + (const vec<T>&, const vec<T>&); 

Update 2: Solution.

This update is incorrect. The sbi solution really worked, I just could not create the operator template as follows.

    template<typename T> 
    vec<T> operator +<T> (const vec<T>&, const vec<T>&); 

sbi, , . , , , +, . , . .

// ...
template<typename T> 
class vec{
    public:
    const vec<T> operator + (const vec<T>&, const vec<T>&)const;
    // ...

}; // Vec.

template<typename T> 
const vec<T> vec<T>::operator + (const vec<T>& v)const{
    matrix<T> product = *this;
    vec(product += v);
} // Addition.

, , , sbi . , , .

. .

+5
2

, , , . - . :

template<typename T> 
class vec;

template<typename T> 
vec<T> operator + (vec<T>, const vec<T>&);

template<typename T> 
class vec{
public:
    friend vec<T> operator +<T> (vec<T>, const vec<T>&);
// ...

operator+(), operator+<T>. ( :

// no forward declarations necessary

template<typename T>
class some_class {
  template<typename U>
  friend void f(vec<U>&);
  // ... 
};

, , .) a >

: ( !), , friend . operator+ - public vec (operator+=) , , friend . ,

template<typename T> 
class vec{
public:
    // ...
};

template<typename T> 
vec<T> operator + (vec<T> a, const vec<T>& b){
    a += b;
    return a;
}

:

  • operator+() ( operator+=(), BTW) .
  • inline, .
  • operator+=() const, , f(m1+=m2) , f() const.
  • . , vec& operator += (const vec&);. ( , , , .)
  • std::vector std::vector<blah>::size_type, unsigned short.
+7

:

inline friend vec<T> operator + (const vec<T>&, const vec<T>&);

:

template<typename T> 
    vec<T>& operator + (const vec<T>& a, const vec<T>& b){
        vec<T> product = a;
        product += b;

        return product;
    } // Addition.
+7

All Articles