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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Header:

#pragma once


template <typename T> 
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<T> operator - (const Coord  <T> &  b1, const Coord  <T> & 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  <T> const &  c)
	 {
		 x_ = c.getX(); 
	 }


	 void setY(Coord  <T> 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 <iostream>
# include "coord.hpp"
using namespace std;


//template <typename T>
//Coord<T> operator - (const Coord  <T> &  b1, const Coord  <T> & b2) {
//	Coord <T> b3;
//	b3.x_ = b1.x_ - b2.x_;
//	b3.y_ = b1.y_ - b2.y_;
//	return b3;
//}


//template <typename T>
//Coord<T> operator - ( Coord  <T> &  b1,  Coord  <T> & b2) {
//	Coord <T> b3;
//	b3.x_ = b1.getX() - b2.getX();
//	b3.y_ = b1.getY() - b2.getY();
//	return b3;
//}



template <typename T>
Coord<T> operator - (Coord  <T> &  b1, Coord  <T> & b2) {
	Coord <T> b3;
	b3.setX( b1.getX() - b2.getX() );
	b3.setY( b1.getY() - b2.getY() );
	return b3;
}



template <typename T>
ostream & operator << (ostream & os,  Coord <T> & c)
{
	os << "(" << c.getX() << "," << c.getY() << ")" << endl; 
	
	return os; 

}



int main()
{
	Coord <double>  a(3, 3);
	Coord <double> b(2, 2);

	Coord <double> c;

	c = a - b; 

	cout << "c is " << c << endl; 

	int magnitude;
	magnitude = c.magnitude(c);

	cout << " The magnitude of c is " << magnitude << endl;

	Coord <double> 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.
1
2
3
4
    void setX( T x )
    {
        x_ = x; 
    }


Or maybe do something like this:
1
2
3
4
5
6
7
template <typename T>
Coord<T> operator - (const Coord  <T> &  b1, const Coord  <T> & b2) 
{
	Coord <T> b3 ( b1.getX() - b2.getX(), b1.getY() - b2.getY() );
	
	return b3;
}

Last edited on
Registered users can post here. Sign in or register to post.