c++编译问题

C++ compiling Issue

本文关键字:问题 编译 c++      更新时间:2023-10-16

当我尝试编译时,我一直有一个错误给我的问题。错误显示为error: no matching function for call to 'Invoice::Invoice(const char [10], double, int)'它在

上给我错误
//create an invoice using constructor with parameters
    Invoice ductTape("Duct Tape",2.99,10);

这是我的代码,第一个你需要把它保存为发票。它花了我一段时间来实际修复大多数错误。这是我唯一的错误。

#include <iostream>
#include <string>
using namespace std;
class Invoice
    {
    public:
        void setDescription(string bagofhammers)
        {
    description = "bag of hammers";
        }
        void setQuantity(int)
        {
            quantity = 1;
        }
        void setPrice (int)
        {
            price = 12.99;
        }
        void ductTape()
        {
            setDescription("Duct Tape");
            setPrice(2.99);
            setQuantity(10);
        }
        string getDescription(string description)
        {
            return description;
        }
        int getQuantity(int quantity)
        {
            return quantity;
        }
        int getPrice(double price)
        {
            return price;
        }
        void print() 
        { 
            std::cout << "Invoiced item is: " << getDescription(description) << endl;
            std::cout << "Quantity ordered: "<< getQuantity(quantity) << endl;
            std::cout << "Each unit's price is: "<< getPrice(price) << endl;
            std::cout << "Total Amount: "<< (getPrice(price)*getQuantity(quantity)) << endl;
        }

    private:
        string description;
        double price;
        int quantity;
    };

这个是将要使用它的程序。

#include <iostream>
#include "Invoice.h"
using namespace std;
int main() {
    string description;
    double price;
    int quantity;
    cout << "Enter the description: ";
    getline(cin, description);
    cout << "Enter the unit price: ";
    cin >> price;
    cout << "Enter the quantity: ";
    cin >> quantity;
    cout << endl;//a new line
    //create an invoice using default constructor
    Invoice hammers;
    hammers.setDescription(description);
    hammers.setPrice(price);
    hammers.setQuantity(quantity);
    //now print the invoice
    hammers.print();
    cout << endl;
    //create an invoice using constructor with parameters
    Invoice ductTape("Duct Tape",2.99,10);
    cout << "[Invoice for object created using constructor]" <<endl;
    ductTape.print();
    cin.ignore(255,'n');//ignore any leftover new lines
    cin.get();//pause the output...
    return 0;
}

我想我把ductape部分搞砸了。你必须记住,这是我第一次学c++。所以,如果你不介意解释一下这个错误,希望我能从中吸取教训。

您缺少构造函数。添加到header

中的公共部分
Invoice() :
description(0), price(0), quantity(0) {}
Invoice(const char* _description, double _price, int _quantity) :
description(_description), price(_price), quantity(_quantity) {}

错误消息非常清楚。你需要一个Invoice类的构造函数。

Invoice::Invoice(const char* description_, double price_, int quantity_)
 : description(description_), price(price_), quantity(quantity_)
{}

这个构造函数使用初始化列表来初始化带有构造函数参数的成员变量。

编辑:作为@the。malkolm指出,通过定义Invoice hammers;

,您还可以在代码中使用Invoice的默认构造函数

既然实现了一个接受参数的构造函数,那么现在还需要实现默认构造函数。

Invoice::Invoice()
  : description(0), price(0), quantity(0)
{}

您没有编写构造函数

ie. Invoice() { ....} and Invoice(const char * a, double b, int c) {....}

您的Invoice类中没有正确的构造函数