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'

Recommended Answers

All 2 Replies

#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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.