This function does not change its class; it does nothing. Additionally, why not use the constructor you made?
31 32 33 34 35 36 37 38 39 40
Coordinate Coordinate::add(const Coordinate b)
{
Coordinate f;
f.x = x + b.x;
f.y = y + b.y;
f.z = z + b.z;
return f;
}
You should pass b by reference. Additionally, why not use the constructor you made?
42 43 44 45 46 47 48 49 50 51
Coordinate Coordinate::subtract(const Coordinate b)
{
Coordinate e;
e.x = x - b.x;
e.y = y - b.y;
e.z = z - b.z;
return e;
}
You should pass b by reference and use the constructor you made. Additionally, why did you avoid giving e the same name as f from the other function? They are in separate functions; they can't interfere with each other.
blaze41 wrote:
Its giving me all kinds of errors and what not.
Care to actually tell us what these errors are? You should copy and paste them ;)
In this example, you want to set the values of this object, not some temp object, so the correct code is:
1 2 3 4 5 6
void Coordinate::setCoordinate(double xx, double yy, double zz)
{
x = xx;
y = yy;
z = zz;
}
Similarly, add and subtract must modify the object. A few tweaks, you pass in the value to add by const reference rather than by value. You don't have to return a value, but if you do, it's conventional to return the actual object under modification by reference.
1 2 3 4 5 6 7 8
Coordinate& Coordinate::add(const Coordinate &b)
{
x = x + b.x;
y = y + b.y;
z = z + b.z;
return *this;
}
Thank you for your reply, how would I use the constructor to add and subtract the different coords?
this is the error i get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
g++ Coordinate.h Coordinate.cpp pa8.cpp
Coordinate.cpp: In member function `void Coordinate::setCoordinate(double, double, double)':
Coordinate.cpp:21: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note: Coordinate::Coordinate(double, double, double)
Coordinate.cpp: In member function `Coordinate Coordinate::add(Coordinate)':
Coordinate.cpp:36: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note: Coordinate::Coordinate(double, double, double)
Coordinate.cpp: In member function `Coordinate Coordinate::subtract(Coordinate)':
Coordinate.cpp:47: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.cpp:13: note: Coordinate::Coordinate(double, double, double)
pa8.cpp: In function `int main()':
pa8.cpp:9: error: no matching function for call to `Coordinate::Coordinate()'
Coordinate.h:10: note: candidates are: Coordinate::Coordinate(const Coordinate&)
Coordinate.h:16: note: Coordinate::Coordinate(double, double, double)
You wouldn't, because you shouldn't have been making another obejct in the first place. I just mentioned it as an additional point because you took the time to make the nice convenient constructor but then you completely circumvented it.
The constructor should only initialize the members of the class, nothing else. Similarly, the destructor should only uninitialize the members of the class. All the work must be done by class methods.
As for the compiler errors:
You don't have a default constructor, so you can write code like:
Coordinate c;
But that's ok.
The real problem is that your function void Coordinate::setCoordinate(double, double, double) is still creating a temporary object. I've already shown that this is incorrect.
Thank you kbw for your help! I don't fully understand what a default constructor is. I read the classes (I) tutorial but I guess im not that smart would a default constructor be something like: Coordinate(double xx, double yy, double zz) {x=xx; y=yy; z=zz;}; ?
A default constructor is the constructor that is called when you don't specify any arguments.
In all your legitimate code you always specify the coordinate values. It's only in your incorrect code that don't provide coordinates. So don't worry about creating a default constructor as it will make your incorrect code compile, but the results will be incorrect.
this is a pointer to the object currently being dealt with; the object on which the member function was called. To return a reference you have to dereference the this pointer. (Remember that dereferencing a pointer is unrelated to C++ references)
add and subtract don't need to return anything, void is sufficient.
In C, it's common to see code like:
a = b = c = 0;
When C++ was developed a similar feature was designed for the assignments operator. So you can do that sort of thing with objects in C++ and it requires the assignment operator to return *this to allow this. But like I said, you don't need it.