0

Hello everybody!
I'm trying to display the result of addition of vectors. But Builder shows the error after compilation (line 29). What should I change for in my code in order to find the problem?
Thanks in advance.

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class CVector2D {
  public:
    CVector2D(double x0 = 0, double y0 = 0)
    :x(x0), y(y0)
{
}

double x, y;

CVector2D const operator +(CVector2D const& vector2)const {
return CVector2D(x + vector2.x, y + vector2.y);
}

};

using namespace std;

int main()
{

CVector2D a(3.0, 5.8);
CVector2D b(7.3, 8.8);

CVector2D c = a + b + CVector2D(3, 9);
cout << "Sum is equal " << c << endl;

getch();
return 0;
}

Error (line 29):

[C++ Error] 5.cpp(27): E2094 'operator<<' not implemented in type 'ostream' for arguments of type 'CVector2D'

2
Contributors
2
Replies
8
Views
3 Years
Discussion Span
Last Post by Auroch
0
#include <iostream.h>

The .h is not recommended. Remove it.

#include <iomanip.h>

What are you using this for? You don't need it.

#include <conio.h>

This library is not well supported. You program won't even run on a modern compiler. People stopped using it 20 years ago. On top of that, you're not even using it. Remove it.

class CVector2D {
public:
    CVector2D(double x0 = 0, double y0 = 0)
    : x(x0), y(y0)
    {}

    double x, y;

    CVector2D const operator +(CVector2D const& vector2)const
    {
        return CVector2D(x + vector2.x, y + vector2.y);
    }
};

using namespace std;
int main()
{
    CVector2D a(3.0, 5.8);
    CVector2D b(7.3, 8.8);
    CVector2D c = a + b + CVector2D(3, 9);
    cout << "Sum is equal " << c << endl;
    getch();
    return 0;
}

Onfortunatly, ostream::operator<< has no idea what a CVector2D" is! You'll need to overload it. An example can be seen here.

Edited by Hiroshe

0

Thank you so much, Hiroshe! Your piece of advice is very helpful.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.