【问题标题】:Why doen't my code work? c++ operator [duplicate]为什么我的代码不起作用? c ++运算符[重复]
【发布时间】:2016-05-22 13:18:57
【问题描述】:
 //main()
  #include <iostream>
 #include "Circle.h"
 #define PI 3.1415965 //defining the pi number

 using namespace std;         //I want to create a class that has the characteristics of a circle

  int main()
 {
    Circle c1(1);   //making a class object
     cout<<c1;      //using the operator<< after overloading it 

       return 0;
 }

   //circle.h
  #include <iostream>          //I'm just practicing with these things so my code will probably have some mistakes but I really cannot understand where is the right place for the operator<< because I receive errors all the time
  using namespace std;


      class Circle    //creating the class circle
{
        public:
          Circle();                //constructor with zero members
          Circle(float r);         //constructor with one member
          float getPerimetre();      //getting the perimeter of the circle
          float getArea();          //getting the area of the circle
          friend ostream &operator<<(ostream &mystream, Circle &p);   //making the operator<<

        private:
          float radius;  //private members
};


     #endif // CIRCLE_H

     //circle.cpp
      #include "Circle.h"
     #include <iostream>
     #define PI 3.14159265   //defining the pi number

     using namespace std;

      Circle::Circle()    //creating the constructor with zero members
     {
         radius=0;   

     Circle::Circle(float r)    //creating the constructor with one member
     {
         radius=r;
      }
     float Circle::getPerimetre()    //explaining the functions  get perimetre
     {
         return (2*PI*radius);
      }
     float Circle::getArea()  //and get area
      {
          return (PI*radius*radius);
      }
      ostream &operator<<(ostream &mystream, Circle &p)  //i'm not sure if this is the right place to write this
      {
            mystream<<radius<<", "<<getPerimetre()<<", "<<getArea()<<endl;
            return mystream;
      }

从我读过的所有内容中,我真的无法理解编写此运算符的正确位置以及为什么我在运行项目时不断收到错误消息。我是新手,也是这个网站的新手,所以任何帮助都会非常感激

【问题讨论】:

    标签: c++ operator-overloading


    【解决方案1】:

    您的operator&lt;&lt; 位置不错,但是由于它不是成员函数,您无法在没有对象的情况下访问成员

    p.radius 而不是radius

    p.getPerimetre() 而不是getPerimetre()

    p.getArea() 而不是getArea()

    ostream &operator<<(ostream &mystream, Circle &p)  //i'm not sure if this is the right place to write this
              {
                    mystream<<radius<<", "<<p.getPerimetre()<<", "<<p.getArea()<<endl;
                    return mystream;
              }
    

    【讨论】:

      【解决方案2】:
            friend ostream &operator<<(ostream &mystream, Circle &p);   
      

      应该是

            friend ostream &operator<<(ostream &mystream, const Circle &p);   
      

      实现应该是这样的

        ostream &operator<<(ostream &mystream, const Circle &p)
        {
              mystream<<p.radius<<", "<<p.getPerimetre()<<", "<<p.getArea()<<endl;
              return mystream;
        }
      

      这还要求您在类声明中将 getPerimetre()getArea() 归类为 const 函数

       class Circle {
          public:
            // ...
            float getPerimetre() const;
                              // ^^^^^
            float getArea() const;
                         // ^^^^^
            // ...
       };
      

      和定义

       float Circle::getPerimetre()  const {
                                  // ^^^^^
      
           return (2*PI*radius);
        }
       float Circle::getArea() const {
                            // ^^^^^
            return (PI*radius*radius);
        }
      

      【讨论】:

        猜你喜欢
        • 2015-07-06
        • 2017-03-16
        • 1970-01-01
        • 1970-01-01
        • 2013-02-08
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        相关资源
        最近更新 更多