当我尝试编译时,我遇到了一个错误问题。错误说,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);
这是我的代码,首先你需要将它保存为Invoice.h。我花了一些时间来实际修复大部分错误。只是,这是我唯一的错误。
#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;
}
我会假设我在ductTape部分拧了一些东西。你必须记住,这是我第一次使用C ++。所以,如果你不介意解释这有什么问题,希望我可以从中学习。
答案 0 :(得分:7)
你缺少构造函数。添加到标题
中的公共部分Invoice() :
description(0), price(0), quantity(0) {}
Invoice(const char* _description, double _price, int _quantity) :
description(_description), price(_price), quantity(_quantity) {}
答案 1 :(得分:4)
错误信息非常清楚。您需要Invoice
类的构造函数。
Invoice::Invoice(const char* description_, double price_, int quantity_)
: description(description_), price(price_), quantity(quantity_)
{}
此构造函数使用初始化列表来使用构造函数参数初始化成员变量。
编辑:正如@ the.malkolm所指出的那样,通过定义Invoice
Invoice hammers;
的默认构造函数
由于您实现了一个带有arugments的构造函数,因此您现在还需要实现默认构造函数。
Invoice::Invoice()
: description(0), price(0), quantity(0)
{}
答案 2 :(得分:1)
您的Invoice
课程中没有正确的构造函数。
答案 3 :(得分:1)
您尚未编写构造函数
ie. Invoice() { ....} and Invoice(const char * a, double b, int c) {....}