Sorry about the formatting.
It still doesnt seem to like my equal operator.
test.cpp:11: error: no match for ‘operator=’ in ‘c = Position<Type>:
perator+(Position<Type>&) [with Type = int](((Position<int>&)(& b)))’
position.h:61: note: candidates are: const Position<Type>& Position<Type>:
perator=(Position<Type>&) [with Type = int]
Thanks alot
Heres all the code:
test.c
Code:
#include <iostream>
using namespace std;
#include "position.h"
int main() {
Position <int> a(1,2,3);
Position <int> b(5,5,5);
Position <int> c;
c = (a + b);
cout << c.getX() << endl;
cout << c.getY() << endl;
cout << c.getZ() << endl;
return 0;
}
position.h
Code:
#ifndef _POSITION_H_
#define _POSITION_H_
template <class Type> class Position {
public:
//
// Constructors
//
Position() { // 0d
set(0, 0, 0);
}
Position(Type X) { // 1d
set(X, 0, 0);
}
Position(Type X, Type Y) { // 2d
set(X, Y, 0);
}
Position(Type X, Type Y, Type Z) { // 3d
set(X, Y, Z);
}
Position(const Position &rhs) { // Copy constructor
setX( rhs.x );
setY( rhs.y );
setZ( rhs.z );
}
//
// Operators
//
Position operator + (Position <Type> &rhs) {
Position <Type> tmp;
tmp.setX( getX() + rhs.getX() );
tmp.setY( getY() + rhs.getY() );
tmp.setZ( getZ() + rhs.getZ() );
return tmp;
}
const bool operator == (Position &rhs) {
if(getX() != rhs.getX()) {
return false;
}
if(getY() != rhs.getY()) {
return false;
}
if(getZ() != rhs.getZ()) {
return false;
}
return true;
}
const Position& operator = (Position <Type> &rhs) {
if (this != &rhs) {
setX( getX() + rhs.getX() );
setY( getY() + rhs.getY() );
setZ( getZ() + rhs.getZ() );
}
return *this;
}
//
// Set position - 1d
//
void set(Type X) { // If a dimension isnt specified
setX(X); // X will be used.
}
void setX(Type X) {
x = X;
}
void setY(Type Y) {
y = Y;
}
void setZ(Type Z) {
z = Z;
}
//
// Set position - 2d
//
void set(Type X, Type Y) {
setX(X);
setY(Y);
}
//
// Set position - 3d
//
void set(Type X, Type Y, Type Z) {
setX(X);
setY(Y);
setZ(Z);
}
//
// Get position
//
const Type get() { // If a dimension isnt specified
return getX(); // X will be used.
}
const Type getX() {
return x;
}
const Type getY() {
return y;
}
const Type getZ() {
return z;
}
//
// General
//
virtual void reset() {
set(0, 0, 0);
}
private:
Type x;
Type y;
Type z;
};
#endif