[英]Error: no matching function for call to constructor in C++
我正在制作一個小程序,它使用構造函數、類、對象和繼承來顯示所選項目的價格。 但是,派生類中的兩個不同構造函數出現兩個錯誤,我該怎么做才能解決這個問題?
#include<iostream>
using namespace std;
class Beverage{
public:
int cost_of_water, cost_of_sugar;
Beverage(int x, int y){
cost_of_water = x;
cost_of_sugar = y;
}
int computeCost(){
}
void print(){
cout<<"The cost of the beverage is: ";
}
};
class Tea: public Beverage{
public:
int price_of_tea_leaves;
Tea(int a){
price_of_tea_leaves = a;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_tea_leaves;
return cost;
}
};
class Coffee: public Beverage{
public:
int price_of_coffee_powder;
Coffee(int b){
price_of_coffee_powder = b;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_coffee_powder;
return cost;
}
};
int main(){
int m,n;
cout<<"*****Welcome to the cafeteria management system*****";
cout<<"1 FOR TEA, 2 FOR COFFEE";
cin>>m;
if(m = 1){
Beverage B(10,5);
Tea T(10);
B.print();
T.computeCost();
}
else if (m = 2){
Beverage B(10,5);
Coffee C(15);
B.print();
C.computeCost();
}
else{
cout<<"Thank You!";
}
}
所以,這是運行良好的代碼:
#include<iostream>
using namespace std;
class Beverage{ //base class
public:
int cost_of_water, cost_of_sugar;
Beverage(int x, int y){ //base class constructor
cost_of_water = x;
cost_of_sugar = y;
}
int computeCost(){
}
};
class Tea: public Beverage{ //derived class
public:
int price_of_tea_leaves;
Tea(int a):Beverage(10,5){ //derived class constructor
price_of_tea_leaves = a;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_tea_leaves;
return cost;
}
void print(){
cout<<"The cost of the tea is: "<<computeCost();
}
};
class Coffee: public Beverage{ //derived class
public:
int price_of_coffee_powder;
Coffee(int b):Beverage(10,5){ //derived class constructor
price_of_coffee_powder = b;
}
int computeCost(){
int cost = cost_of_sugar + cost_of_water + price_of_coffee_powder;
return cost;
}
void print(){
cout<<"The cost of the coffee is: "<<computeCost();
}
};
int main(){
int m,n;
cout<<"*****Welcome to the Cafeteria management system*****"<<endl;;
cout<<"Input 1 for TEA and 2 for COFFEE: ";
cin>>m;
if(m == 1){
Beverage B(10,5);
Tea T(10);
T.print();
}
else if (m == 2){
Beverage B(10,5);
Coffee C(25);
C.print();
}
else{
cout<<"ByeBye!";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.