0

次のエラーが表示されます。

circleType.cpp||In function 'std::ostream& operator<<(std::ostream&, circleType&)':|
circleType.cpp|48|error: 'getRadius' was not declared in this scope|
circleType.cpp|49|error: 'circumference' was not declared in this scope|
circleType.cpp|50|error: 'area' was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|

エラーは次の場所にあります。

ostream& operator <<(ostream& outs, circleType& circle1)
{
   outs << "radius: " << getRadius() << endl
   << "circumference: " << circumference() << endl
   << "area: " << area() << endl;
   return outs;
}

例として、円周関数を次に示します。

double circleType::circumference()
{
    return (radius*2*pi);
}

ヘッダー ファイル:

class circleType
{
public:
    circleType(); // færibreytulaus smiður
    circleType(double the_radius);
    double getRadius();
    void setRadius(double the_radius);
    double area();
    double circumference();
    friend ostream& operator <<(ostream& outs, circleType& );
private:
    double radius;
};

主要:

circleType circle1(3.0);
cout << "circle1:" << endl;
cout << "--------" << endl;
cout << circle1 << endl;

すべてのヘッダーがどこにでも含まれています。私はまだオーバーロード関数について少し混乱しています。

4

1 に答える 1

3

You are not invoking the member functions on the input object (circle1); rather, you are trying to invoke some global functions with the same names that do not exist (mind the fact that a friend function is not a member function of the class it is a friend of, but rather a free, non-member function that has been given access to the class's internals).

To fix the problem, change the definition of your overloaded operator << as follows:

ostream& operator << (ostream& outs, circleType& circle1)
{
   outs << "radius: " << circle1.getRadius() << endl
   << "circumference: " << circl1.circumference() << endl
   << "area: " << circle1.area() << endl;
   return outs;
}
于 2013-02-22T21:39:07.360 に答える