2016-05-22 108 views
-2
//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; 
     } 
從一切我讀過我真的不明白的地方是正確的位置寫這個運營商,爲什麼我一直當我收到錯誤的事情

出運行該項目。我是新來這一點,也給這個網站,所以任何幫助將是非常apreciated爲什麼我的代碼無法工作? C++運算符

回答

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

operator<<是好地方,但因爲它不是成員函數不能沒有對象訪問成員:

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; 
      } 
相關問題