简体   繁体   English

错误:在 C++ 中没有用于调用构造函数的匹配函数

[英]Error: no matching function for call to constructor in C++

I am making a small program that shows the price of the selected items using constructors, classes, objects and inheritance.我正在制作一个小程序,它使用构造函数、类、对象和继承来显示所选项目的价格。 However, I am getting two errors for two different constructors in the derived classes, what can I do to solve this problem?但是,派生类中的两个不同构造函数出现两个错误,我该怎么做才能解决这个问题?

#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!";
    }
}

So, here is the well functioning code:所以,这是运行良好的代码:

#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.

相关问题 C ++:构造函数没有匹配的函数调用? - C++: no matching function call for constructor? 没有匹配的 function 用于在构造函数中调用 - C++ 11 - No matching function for call in constructor - C++ 11 初学者 C++,构造函数中“没有匹配的 function 调用” - Beginner C++, "no matching function call to" in constructor 在另一个构造函数中调用构造函数(没有匹配函数来调用...)c ++ - Call constructor inside another constructor (no matching function to call…) c++ c ++:函数“没有匹配的函数可调用”错误 - c++ : “no matching function for call to” error with function c++ 代码中的错误“预期的构造函数、析构函数或在 '(' 标记之前的类型转换”和“没有匹配的 function 调用...” - error in c++ code “expected constructor, destructor, or type conversion before ‘(’ token” and “no matching function for call to …” C++:没有匹配的 function 用于调用:为什么需要一个空的构造函数? - C++: no matching function for call: why is an empty constructor needed? C ++中的“没有匹配函数可调用X”错误 - “No matching function for call to X” Error in C++ 在C ++中发生没有匹配的函数来调用错误 - occur no matching function for call to error in c++ C ++错误没有匹配的调用函数 - C++ ERROR No matching function for call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM