c++ - Overloading Sum Operator in Custom Vector Class -


as exercise learn little more dynamic memory allocation in c++, i'm working on own vector class. i'm running little difficulty overloading sum operator, thought i'd turn here insight why doesn't work. here's have far:

template<typename t> class vector { private:     t* pointer_;     unsigned long size_; public:     // constructors , destructors.     vector();     template<typename a> vector(const a&);     template<typename a> vector(const a&, const t&);     vector(const vector<t>&);     ~vector();      // methods.     unsigned long size();     t* begin();     t* end();     vector<t>& push_back(const t&);       // operators.     t operator[](unsigned long);     vector<t>& operator=(const vector<t>&);     friend vector<t>& operator+(const vector<t>&, const vector<t>&); }; 

the template<typename a> vector(const a&) constructor looks this:

template<typename t> template<typename a> vector<t>::vector(const a& size) {     this->pointer_ = new t[size];     this->size_ = size; } 

finally, operator+ operator looks this:

template<typename t> vector<t>& operator+(const vector<t>& lhs, const vector<t>& rhs) {     vector<t> result(lhs.size_);     (unsigned long = 0; != result.size_; i++)     {         result.pointer_[i] = lhs.pointer_[i] + rhs.pointer_[i];     }     return result; } 

my compiler (vs2013) returns unresolved external symbol error when try compile code, (as understand it) means there's function declared somewhere haven't defined. i'm not sure problem is, however: template<typename a> vector(const a&) constructor works fine. missing?

you're not friending template operator function. there multiple ways this, each of has advantages. 1 friend-the-world ideology expansions of template friends each other. prefer bit more restrictive that.

above vector class, this:

template<typename t> class vector;  template<typename t> vector<t> operator+(const vector<t>& lhs, const vector<t>& rhs); 

the proceed before, note syntax in friend declaration:

// vector class goes here.... template<typename t> class vector {     .... stuff ....     friend vector<t> operator+<>(const vector<t>&, const vector<t>&); }; 

then define rest have it. should think you're trying achieve.

best of luck.

ps: invalid reference return value fixed in above code.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -