Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+;中的调用没有匹配函数+; 我试图学习C++中的类概念。我编写了一些代码来测试我所知道的,在编译代码时,第一个错误是:“没有匹配的函数用于调用'base::base()' base1、base2_C++_Class - Fatal编程技术网

C+;中的调用没有匹配函数+; 我试图学习C++中的类概念。我编写了一些代码来测试我所知道的,在编译代码时,第一个错误是:“没有匹配的函数用于调用'base::base()' base1、base2

C+;中的调用没有匹配函数+; 我试图学习C++中的类概念。我编写了一些代码来测试我所知道的,在编译代码时,第一个错误是:“没有匹配的函数用于调用'base::base()' base1、base2,c++,class,C++,Class,我不知道为什么 以下是全部代码: #include <iostream> using namespace std; class base { int x, y; public: base (int, int); void set_value (int, int); int area () { return (x*y); } }; base::base ( int a, int b) { x = a; y = b; } void base::set_valu

我不知道为什么

以下是全部代码:

#include <iostream>
using namespace std;

class base {
   int x, y;
  public:
  base (int, int);
  void set_value (int, int);
  int area () { return (x*y); }
};
base::base ( int a, int b) {
 x = a;
 y = b;
}
void base::set_value (int xx, int yy) {
 x = xx;
 y = yy;
}
int main () {
base base1, base2;
base1.set_value (2,3);
base2.set_value (4,5);
cout << "Area of base 1: " << base1.area() << endl;
cout << "Area of base 2: " << base2.area() << endl;
cin.get();
return 0;
}
#包括
使用名称空间std;
阶级基础{
int x,y;
公众:
基(int,int);
void set_值(int,int);
int区域(){return(x*y);}
};
base::base(inta,intb){
x=a;
y=b;
}
空基::设置_值(整数xx,整数yy){
x=xx;
y=yy;
}
int main(){
碱基1,碱基2;
base1.set_值(2,3);
base2.set_值(4,5);
你可以用

base base1, base2;
只有当有办法使用默认构造函数
base
时。由于
base
已明确定义了非默认构造函数,因此默认构造函数不再可用

您可以通过以下几种方式解决此问题:

  • 定义默认构造函数:

    base() : x(0), y(0) {} // Or any other default values that make more
                           // sense for x and y.
    
  • 在构造函数中提供参数的默认值:

    base(int a = 0, int b = 0);
    
  • 使用有效参数构造这些对象

    base base1(2,3), base2(4,5);
    
    base base1(2,3), base2(4,5);
    

  • base base1,base2;
    尝试使用
    base
    的默认构造函数来构造两个
    base
    对象(也就是说,
    base::base()
    base
    没有默认构造函数,因此不会编译

    要解决此问题,请将默认构造函数添加到
    base
    (声明并定义
    base::base()
    ),或使用已定义的双参数构造函数,如下所示: