1

I'm trying to use separate files for my project, including header file, which declares class methods and .cpp file for defining methods.

But, enforcing hidden method implementation I get errors and can't compile code.

File vector.h

#ifndef VECTOR_H
#define VECTOR_H


#include <iostream>

class Point
{
private:
    float x;
    float y;

public:
    Point(float x, float y);
    float get_x() const;
    float get_y() const;

};

#endif // VECTOR_H

File vector.cpp

#include "vector.h"

Point::Point(float x, float y): x(x), y(y) {}

float Point::get_x() const
{
    return x;
}

float Point::get_y() const
{
    return y;
}

Point operator+(Point& pt1, Point& pt2)
{
    return {pt1.get_x() + pt2.get_x(), pt1.get_y() + pt2.get_y()};
}

std::ostream& operator<<(std::ostream& os, const Point& pt)
{
    os << '(' << pt.get_x() << ', ' << pt.get_y() << ')';
    return os;
}

File source.cpp

#include "vector.h"

int main()
{
    Point p1(1.4, 2.324), p2(2.004, -4.2345);

    std::cout << p1 << '\n';
    std::cout << p2 << '\n';
    std::cout << p1 + p2 << '\n';

    return 0;
}

In the end I get:

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'Point')

error: no match for 'operator+' (operand types are 'Point' and 'Point')
Axiumin_
  • 2,107
  • 2
  • 15
  • 24
Pr.Germux
  • 55
  • 5
  • 3
    I don't see the `<<` operator being declared in the header file. Ditto for the `+` operator. It is defined in the `.cpp` file, but the whole purpose of header files is to declare stuff that's defined in header files, so that other translation units know that they exist, right? How do you expect one `.cpp` to know about a custom operator or a function that's defined in another `.cpp`, unless it gets explicitly declared somewhere. Oh, and the operators better take `const` parameters too, otherwise they won't work with temporary objects. – Sam Varshavchik Aug 13 '19 at 20:30
  • 1
    Missing `const` for `Point operator+(Point& pt1, Point& pt2)`, -> `Point operator+(const Point& pt1, const Point& pt2)` – Jarod42 Aug 13 '19 at 20:42

1 Answers1

3

You have a compile error for your main knows nothing about operator+ and operator<<.

Write

Point operator+(Point& pt1, Point& pt2);
std::ostream& operator<<(std::ostream& os, const Point& pt);

in h file or forward declare them in the main file.

One more thing. You should use "" in << ", " <<.

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
Nestor
  • 687
  • 5
  • 12
  • 1
    Prefer to add the declarations to the header over placing them directly in main. The header is more useful going forward. – user4581301 Aug 13 '19 at 20:41
  • Why I can't add `Point operator+(Point& pt1, Point& pt2); std::ostream& operator<<(std::ostream& os, const Point& pt);` lines in `public` of class? – Pr.Germux Aug 13 '19 at 21:00
  • @Pr.Germux, you may. Yet the signature will be different. I'll post an answer in a moment. – Nestor Aug 13 '19 at 21:14
  • @Pr.Germux, it will be `Point operator+(const Point& lhs) const` for the `operator+`. By the way, make everything which is logically `const` actually `const`. – Nestor Aug 13 '19 at 21:21
  • @Pr.Germux, for the `operator<<`, read https://stackoverflow.com/questions/9351166/does-overloading-operator-works-inside-the-class – Nestor Aug 13 '19 at 21:23