dimanche 5 avril 2020

Issues with overloading the << operator

I'm currently trying to playaround with operator overloading and built the following code

//Circle.cpp

//Circle.cpp


#ifndef CIRCLE_H
#define CIRCLE_H


#include "IOD.cpp"
#include<string>

class IOD;  //Forward declaration

class Circle
{
   public:
      void display(IOD& ioDevice) const
      {
           ioDevice<<*this;
      }
};

#endif

//IOD.cpp

//IOD.cpp
#ifndef IOD_H
#define IOD_H

#include <iostream>
#include<string>
#include "Circle.cpp"

class Circle;  //Forward declaration


class IOD
{
    // Interface for displaying CAD objects
public:
    void operator<<(const Circle& c)
    {
        std::cout << "Displaying the object Circle  Using IODevice GraphicsScreen for circles";

    }


};

#endif

//Source.cpp

//Source.cpp
#include <iostream>
#include "Circle.cpp"
#include "IOD.cpp"

int main()
{
    Circle* c1 = new Circle();
    IOD* d1 = new IOD();

    c1->display(*d1);
}

I'm trying to have the display function in Circle to call the overloaded operator << in IOD to print output about the Circle object to the screen. My understanding is that using "ioDevice<<*this" inside the display function should produce the desired output. But instead i'm getting the following error

error C2676: binary '<<': 'IOD' does not define this operator or a conversion to a type acceptable to the predefined operator

Any workarounds to this?

Aucun commentaire:

Enregistrer un commentaire