problem: operator overload with classes

Hi all,

I made some code that dosent work.
I wrote it first for the stack, that means without "new" and "delete" and pointers. And it worked.
The Problem is the Substract-Funktion. I

Its written in Visual Studio 2005, Windows XP.
The Error says:
error C2440: '=': '__w64 int' cannot be converted into 'Point *'

Here is the code:

// Collision_Detection_Suite.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//


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

using namespace std;

/////////////////////////////////////////////////////////////////////////
// Point Class ////////////////////////////////
//
#include "stdafx.h"
#include <iostream>

using namespace std;

class Point
{
float x,y,z;
public:
Point(void);
~Point(void);
Point(float xPos,float yPos,float zPos);

void setCoord(float,float,float);
float* getCoord(void);

Point* operator - (Point*);

};



// Point Class ////////////////////////////////
//
// Konstructors

Point::Point():x(0),y(0),z(0)
{ cout<<"Point erzeugt "<<endl;
cout<<endl;
}


Point::Point(float xPos, float yPos, float zPos):x(xPos),y(yPos) ,z(zPos)
{ cout<<"Point xyz erzeugt "<<endl;
cout<<endl;
}


Point::~Point()
{ cout<<"Point geloescht "<<endl;
cout<<endl;
}


//
// Methods
void Point::setCoord(float xPos,float yPos,float zPos){
x= xPos;
y= yPos;
z= zPos;
}
float* Point::getCoord(void){
float Tapper [3];
Tapper [0] = x;
Tapper [1] = y;
Tapper [2] = z;
return Tapper;
}
//
// Member Functions
//
// Subtract Points
Point* Point::operator - (Point* SubstractPoint){
Point* Buffer;
float xBuffer, yBuffer, zBuffer;
xBuffer = x - SubstractPoint->getCoord()[0];
yBuffer = y - SubstractPoint->getCoord()[1];
zBuffer = z - SubstractPoint->getCoord()[2];
Buffer->setCoord(xBuffer, yBuffer, zBuffer);
return Buffer;
}

int main()
{
Point *A = new Point;
Point *B = new Point(3,4,5);
Point *C = new Point(3,5,7);

A = C - B;

system("pause");
}

/////////////////////////////////////////////////////////////////////////

Please help me out :)

Thx
Malefitz

What line is the error on.
closed account (DSLq5Di1)
1
2
3
4
5
6
7
float* Point::getCoord(void){
    float Tapper [3];
    Tapper [0] = x;
    Tapper [1] = y;
    Tapper [2] = z;
    return Tapper;
}

Tapper is a local variable, and no longer exists once this function returns, the memory it occupied may or may not contain the value(s) expected. Why not create a function to return each coordinate? getCoordX(), getCoordY(), getCoordZ().

Your Subtract function suffers from the same problems, return Point, not a pointer to a Point and make SubstractPoint a constant reference.

1
2
3
4
5
6
7
8
9
10
Point Point::operator - (const Point& SubstractPoint)
{
    Point Buffer;
    float xBuffer, yBuffer, zBuffer;
    xBuffer = x - SubstractPoint.getCoordX();
    yBuffer = y - SubstractPoint.getCoordY();
    zBuffer = z - SubstractPoint.getCoordZ();
    Buffer.setCoord(xBuffer, yBuffer, zBuffer);
    return Buffer;
} 


1
2
3
4
5
Point *A = new Point;
Point *B = new Point(3,4,5);
Point *C = new Point(3,5,7);

A = C - B; // This is SPARTAAA!! I mean err.. pointer arithmetic?! 

I'm scratching my head here with your use of pointers and what you are trying to achieve, dereference them if you want any meaningful result. *A = *C - *B;

Recommended reading:-
http://stackoverflow.com/questions/162941/why-use-pointers
http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c
http://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers
http://stackoverflow.com/questions/4421706/operator-overloading
Last edited on
Topic archived. No new replies allowed.