2

I overloaded a cout and cin opeartor and when I tried to use it, it gave me an error like this:

1 IntelliSense: function "std::basic_ostream<_Elem, _Traits>::basic_ostream(const     std::basic_ostream<_Elem, _Traits>::_Myt &) 
[with _Elem=char, _Traits=std::char_traits<char>]" 
(declared at line 84 of "C:\Program   Files (x86)\Microsoft Visual Studio 12.0\VC\include\ostream") cannot be referenced -- it is a deleted function    

And here's the header file of my class:

#pragma once
#include <iostream>

class Point2D
{
private:
    int m_X;
    int m_Y;
public:
    Point2D(): m_X(0), m_Y(0)
    {
    }

    Point2D(int x, int y): m_X(x), m_Y(y)
    {
    }

    friend std::ostream& operator<< (std::ostream out, const Point2D &point)
    {
        out << "(" << point.m_X << "," << point.m_Y << ")" << std::endl;

        return out;
    }

    friend std::istream& operator>> (std::istream in, Point2D &point) 
    {
        in >> point.m_X;
        in >> point.m_Y;

        return in;
    }

    int getX() const { return m_X; }
    int getY() const { return m_Y; }

    ~Point2D();
};

So, basically, it's just a class that can return and set X and Y coordinates. And I overwrote << and >> operators to make things easier.

But when I try to use it in the main function like this:

#include "stdafx.h"

#include <iostream>
#include "Point2D.h"

using namespace std;

int main(int argc, char * argv[])
{
    Point2D point(7, 7);

    cout << point;  //there's an error here.


    return 0;
}

There seems to be the error on this line:

cout << point; 

What exactly am I doing wrong?

James
  • 176
  • 1
  • 4
  • 16
  • 1
    Those classes are not copyable. See [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) for how to properly overload these operators. – chris Jul 16 '14 at 11:03

3 Answers3

5

The error message says ostream cannot be copied.

friend std::ostream& operator<< (std::ostream out, const Point2D &point)
{
    ...

std::ostream out is pass-by-value: When the operator << is called, compiler tries to copy the argument. However, std::ostream cannot be copied, so compilation is failed.

You should use reference.

friend std::ostream& operator<< (std::ostream &out, const Point2D &point)
{
    ...

friend std::istream& operator>> (std::istream &in, Point2D &point) 
{
    ...

(Even if you're using some classes which provides copying, you shouldn't usually use pass-by-val. Copying some objects is so expensive;)

ikh
  • 10,119
  • 1
  • 31
  • 70
2

Just had the same problem. For other peoples looking for a solution. The error

function "std::basic_ostream<_Elem, _Traits>::basic_ostream(const std::basic_ostream<_Elem, _Traits> &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 83 of "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include\ostream") cannot be referenced -- it is a deleted function

comes from the line:

std::ostream& operator<< (std::ostream os, const Point2D &point)

correct it to this and it should be fine:

std::ostream& operator<< (std::ostream &os, const Point2D &point)
lujobi
  • 44
  • 1
  • 5
0

The problem here is with the arguments

friend std::ostream& operator<< (std::ostream out, const Point2D &point)

The input argument out is pass-by-value. When the compiler tries to copy the std::ostream variable by value it fails and thus the error. Pass the variable out by reference as shown below and it should clear the error. Also, the return type is pass-by-reference, so by doing the below change your are returning the same object and not creating a new copy.

friend std::ostream& operator<< (std::ostream &out, const Point2D &point)

Useful link: How to properly overload the << operator for an ostream?

Shrikanth N
  • 652
  • 3
  • 17