每当我尝试运行此代码时,它都会给我一个错误

whenever i try to run this code it gives me an error

本文关键字:错误 一个 试运行 代码      更新时间:2023-10-16
#include<iostream>
#include<conio.h>
using namespace std;
class one
{
    int r;
public:
    one(int a) : r(a) {}
    void set(int a) { r = a; }
    int area() { return r*r*3.14; }
};
class two
{
    one x;
    int hi;
public:
    two(int r, int h)
    {
        hi=h;
        x.set(r);
    }
    int v() { return x.area()*hi; }
};
int main()
{
    _getch();
    return 0;
}

错误是:没有合适的默认构造函数可用。 你介意帮助我,这样我就可以摆脱这个错误。 //.............................................................................

当你在two中声明x时,C++基本上会尝试使用没有任何参数的构造函数(默认构造函数)创建一个实例,因为"空对象"在C++中不存在。

一个不提供默认构造函数,但它提供了构造函数one(int a),因此使用one x(0);one x = one(0);而不是one x;

相关文章: