operator+ overloading

Hey I've tried to look at many examples but I just can't seem to get it. I'm trying to add 2 vectors and their components using the '+' operator. Here is my code:

//encapsulate.h
#ifndef ENCAPSULATE_H
#define ENCAPSULATE_H
#include <iostream>
using namespace std;
typedef struct Vector
{
float x;
float y;
float z;
};

class Encapsulate {
public:
Encapsulate();
void encapsulate(float x, float y, float z);
void magnitude();
Vector operator+(Vector &a, Vector &b);
Vector v;
Vector vect;
Vector answer;
private:

};

#endif

//encapsulate.cpp
#include <stdio.h>
#include "encapsulate.h"

Encapsulate::Encapsulate() {}

void Encapsulate::encapsulate(float x, float y, float z)
{
v.x = x;
v.y = y;
v.z = z;

vect.x = 1;
vect.y = 2;
vect.z = 3;
}

void Encapsulate::magnitude()
{
}

Vector Encapsulate::operator+(Vector &a, Vector &b)
{
Vector c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.y = a.z + b.z;
return c;
}

int main()
{
Encapsulate e;
e.encapsulate(1, 2, 3);
e.answer = e.vect + e.v;
}


//Errors:
Error 2 error C2804: binary 'operator +' has too many parameters 17
Error 3 error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'Vector' d:\lectures\530_computing2\assignment1\assignment1\encapsulate.cpp 34
Error 4 error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'Vector' d:\lectures\530_computing2\assignment1\assignment1\encapsulate.cpp 34
Error 5 error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'Vector' d:\lectures\530_computing2\assignment1\assignment1\encapsulate.cpp 34
Error 6 error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'Vector' d:\lectures\530_computing2\assignment1\assignment1\encapsulate.cpp 34
Error 7 error C2676: binary '+' : 'Vector' does not define this operator or a conversion to a type acceptable to the predefined operator d:\lectures\530_computing2\assignment1\assignment1\encapsulate.cpp 34
This is from a quick vector2 class I'm working on. Have a look at some of the first operator functions.

http://cplusdev.com/pages/tutorials/Vector2.html
The + operator has two forms for overloading.

As a member function:
MyClass MyClass::operator + ( const MyClass& rhs ) const

As a non-member function:
MyClass operator + ( const MyClass& lhs, const MyClass& rhs )

Good luck!
Topic archived. No new replies allowed.