### conversion problem

When i run this code, I am getting :

Error C2664 'void Coord<double>::setX(const Coord<double> &)': cannot convert argument 1 from 'double' to 'const Coord<double> &' CartesianCoord

for this line :

b3.setX( b1.getX() - b2.getX() );

in my Source.cpp file.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148`` ``````Header: #pragma once template class Coord { public: T x_; T y_; Coord() { x_ = 0.0; y_ = 0.0; } // 2 args constructor Coord(T x , T y ) : x_(x) , y_(y) {} // allows operator - to access private fields outside of class //friend Coord operator - (const Coord & b1, const Coord & b2); // does not work //friend ostream & operator << (ostream & os, const Coord & c); T getX() const { return x_; } T getY() const { return y_; } void setX(Coord const & c) { x_ = c.getX(); } void setY(Coord const & c) { y_ = c.getY(); } double& magnitude( const Coord & c ) { double magnitude = c.x_ * c.x_ + c.y_ * c.y_; magnitude = sqrt(magnitude); return magnitude; } // unary minus operator Coord operator-() { this->x_ = (-1) * x_; this->y_ = (-1) * y_; return *this; } }; Source.cpp: # include # include "coord.hpp" using namespace std; //template //Coord operator - (const Coord & b1, const Coord & b2) { // Coord b3; // b3.x_ = b1.x_ - b2.x_; // b3.y_ = b1.y_ - b2.y_; // return b3; //} //template //Coord operator - ( Coord & b1, Coord & b2) { // Coord b3; // b3.x_ = b1.getX() - b2.getX(); // b3.y_ = b1.getY() - b2.getY(); // return b3; //} template Coord operator - (Coord & b1, Coord & b2) { Coord b3; b3.setX( b1.getX() - b2.getX() ); b3.setY( b1.getY() - b2.getY() ); return b3; } template ostream & operator << (ostream & os, Coord & c) { os << "(" << c.getX() << "," << c.getY() << ")" << endl; return os; } int main() { Coord a(3, 3); Coord b(2, 2); Coord c; c = a - b; cout << "c is " << c << endl; int magnitude; magnitude = c.magnitude(c); cout << " The magnitude of c is " << magnitude << endl; Coord d; d = -c; cout << " d is " << d << endl; } ``````

The `setX()` function expects a parameter of type `Coord <T>`. But you try to pass a parameter of type `T`. You might consider changing the setX() and setY() to match the way you intend to use it. It probably doesn't make a lot of sense to pass a Coord object there in any case.
 ``1234`` `````` void setX( T x ) { x_ = x; }``````

Or maybe do something like this:
 ``1234567`` ``````template Coord operator - (const Coord & b1, const Coord & b2) { Coord b3 ( b1.getX() - b2.getX(), b1.getY() - b2.getY() ); return b3; }``````

Last edited on
Topic archived. No new replies allowed.