-4 c++
当我尝试编译时,我遇到了一个错误问题.错误说,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);
Run Code Online (Sandbox Code Playgroud)
这是我的代码,首先你需要将它保存为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;
};
Run Code Online (Sandbox Code Playgroud)
而这一个是将使用它的程序.
#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;
}
Run Code Online (Sandbox Code Playgroud)
我会假设我在ductTape部分拧了一些东西.你必须记住,这是我第一次使用C++.所以,如果你不介意解释这有什么问题,希望我可以从中学习.
你缺少构造函数.添加到标题中的公共部分
Invoice() :
description(0), price(0), quantity(0) {}
Invoice(const char* _description, double _price, int _quantity) :
description(_description), price(_price), quantity(_quantity) {}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |