Displaying difference of vectors

Hi everybody!
I'm trying to display the result of difference of two-dimensional vectors using friend overloading. Builder shows the following error after compilation (line 40). What should I change for in my code in order to find the problem?
Thanks in advance.

[C++ Error] 05_Part.cpp(40): E2034 Cannot convert 'double' to 'Vector2D'


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
#include <iostream.h>
#include <conio.h>

class Vector2D {

double x, y;

friend Vector2D operator- ( const Vector2D &, const Vector2D & );

public:

friend ostream & operator << (ostream & os, const Vector2D & cv)
  {  os << cv.x << ',' << cv.y;
      return os;
  }

};

Vector2D operator- ( const Vector2D & vector1, const Vector2D & vector2 ) {
        Vector2D vector_tmp = vector1;
        vector_tmp.x -= vector2.x;
        vector_tmp.y -= vector2.y;
        return vector_tmp;
        }

int main()
{
double a1, b1;
double a2, b2;

cout << "Enter the first two-dimensional vector: ";
cin >> a1;
cin >> b1;

cout << "Enter the second two-dimensional vector: ";
cin >> a2;
cin >> b2;

// vector declaration
Vector2D a(a1, b1);
Vector2D b(a2, b2);

cout << "Vector #1: " << a << endl;
cout << "Vector #2: " << b << endl;

// vector difference
Vector2D d = a - b - Vector2D(3.0, 9.0);
cout << "Difference is equal " << d << endl;

getch();
return 0;
}


The code that you have does not tell how to make a Vector2D from two double values. How about a constructor?
How to make constructor for this? Is this concept right? (It is not working)
1
2
3
4
5
6
7
Vector2D Vector2D:: operator- ( const Vector2D & vector ) const
{
        Vector vector_tmp = *this;
        vector_tmp.x -= vector.x;
        vector_tmp.y -= vector.y;
        return vector_tmp;
        }
Last edited on
OK.
Topic archived. No new replies allowed.