【问题标题】:Issues with overloading the << operator重载 << 运算符的问题
【发布时间】:2020-04-06 01:00:39
【问题描述】:

我目前正在尝试使用运算符重载并构建以下代码

//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);
}

我试图让 Circle 中的显示函数调用 IOD 中的重载运算符

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

有什么解决方法吗?

【问题讨论】:

    标签: c++11 operator-overloading


    【解决方案1】:

    文件之间存在循环依赖关系:Circle.cpp 包括 IOD.cpp,反之亦然。

    为了使其正常工作,您应该将cpp 文件拆分为cpp.h,其中.h 将仅包含API 定义。

    您应该将#include 视为“复制粘贴”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多