#include "Coord.h"
#include <iostream>
int main()
{
Coord <double> a(3, 3);
Coord <double> b(2, 2);
Coord <double> c = a - b;
std::cout << "c is " << c << '\n';
int magnitude = c.magnitude(c);
std::cout << "The magnitude of c is " << magnitude << '\n';
Coord <double> d = -c;
std::cout << "d is " << d << '\n';
}
i did solved little bit different than u did , Enoizat. I have implemented setters and I am using setters in my overloaded "-" minus operator. I did not know that u could also use friend to bring in a function that is supposed to be outside the class header inside. That is really elegant way of solving the issue.
i managed to correct the magnitude , but i was just wondering if I am testing the overloaded version of the unary minus operator in the correct manner with the line
d = -c ?
So I don't have to overload the copy assignment operator ? why is that ?
Also why is that i don't have to template the unary minus operator ?
Implicitly-declared copy assignment operator
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class.
Also why is that i don't have to template the unary minus operator ?
Well, you should in my opinion: your code would be more legible. Anyway, if you write ‘anything’ of a function (I mean both declaration and definition) inside the template class, it seems the compiler is able to produce a proper code. I suppose it’s connected with this: http://en.cppreference.com/w/cpp/language/template_argument_deduction
Template argument deduction
In order to instantiate a function template, every template argument must be known, but not every template argument has to be specified. When possible, the compiler will deduce the missing template arguments from the function arguments.