[英]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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.