c++ - 重载的 Circle() 在 C++ Circle 类中不明确

标签 c++

我目前正尝试在 C++ 中创建一个 Circle 类,但是当我编译时,我收到一条错误消息,提示“重载的‘Circle’调用不明确。我是 C++ 的新手,不确定这意味着什么。我一直在使用在这里可以找到一个类似的例子 Class tutorial

#include <iostream>

using namespace std;

const double pi = 3.14159265;

class Circle
{
    private:
            double radius, xpos, ypos, area;

    public:
            Circle(double r, double xposition, double yposition) {
                r = radius;
                xposition = xpos;
                yposition = ypos;
            }

            Circle(double r = 0) {
                radius = r;
                xpos = 0;
                ypos = 0;
            } 

            Circle() {
                radius = 0;
                xpos = 0;
                ypos = 0;
            }

            double getRadius() {return radius;}
            double getX() {return xpos;}
            double getY() {return ypos;}
            double getArea() {return pi*radius*radius;}

            Circle operator+(const Circle& c) {
                Circle circle;
                circle.area = this -> getArea() + c.getArea();
                return circle;
            } 
};


int main()
{
    Circle circ(3,2,1);
    double x = circ.getX();
    cout << x << endl;
    return 0;
}

最佳答案

你的问题是这样的:

Circle(double r = 0);
Circle();

上面的内容已经很明显了,没有什么可说的了。

Circle(double r = 0) 中的默认值使得 Circle() 的使用不明确。


顺便说一句,你的成员变量初始化全错了:

Circle(double r, double xposition, double yposition)
{
    r = radius;
    xposition = xpos;
    yposition = ypos;
}

不是将成员变量设置为输入参数的值,而是将输入参数设置为成员变量的“垃圾”值...

关于c++ - 重载的 Circle() 在 C++ Circle 类中不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26633751/

相关文章:

c++ - 避免 C++ 循环中复杂对象的最小范围效率低下的技术?

c++ - 查找数组中出现次数最多的元素

c++ - 当您被新语言困住时如何学习 C++?

c++ - 如何转换格式化字符串 HH :MM:SS to seconds in C++

c++ - 用于在 C++ 中存储特定模板对象的通用容器

c++ - 来自模板化基类的派生类

c++ - 放大/缩小图像 - QT c++

c++ - MPI 和 move 语义

c++ - 如何访问/修改 OpenCV 中的矩阵元素?为什么 at() 被模板化?

c++ - 关闭另一个进程中互斥锁的句柄