c++中没有匹配的调用函数
No matching function for call in C++
我正在学习c++中类的概念。我写了一些代码来测试我所知道的,当编译代码时,第一个错误是:"没有匹配的函数调用' base::base() '"Base base1, base2;"
我不知道为什么!
完整代码如下:
#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;
}
可以使用
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, base2;
尝试使用base
的默认构造函数(即base::base()
)构造两个base
对象。base
没有默认构造函数,因此无法编译。
要解决这个问题,可以向base
添加一个默认构造函数(声明并定义base::base()
),或者使用您已经定义的2参数构造函数,如下所示:
base base1(2,3), base2(4,5);
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?