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

您没有在输入对象 ( circle1) 上调用成员函数;相反,您正在尝试调用一些不存在的具有相同名称的全局friend函数(请注意,函数不是它是朋友的类的成员函数,而是具有被授予访问班级内部的权限)。

operator <<要解决此问题,请按如下方式更改重载的定义:

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 回答