C++ compiling Issue
I have been having an issue with an error that has been given to me when I try to compile. The error says, error: no matching function for call to 'Invoice::Invoice(const char [10], double, int)'
It is giving me the error on
//create an invoice using constructor with parameters
Invoice ductTape("Duct Tape",2.99,10);
Here is my code, first one you will need to save it as Invoice.h It has taken me a while to actually fix most of the errors. Only, this one is the only error I am having.
#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;
};
And this one is the program that will use it.
#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 >> 开发者_JAVA百科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;
}
I would assume that I screwed something up on the ductTape part. You must keep in mind, this is the first time I'm taking C++. So if you don't mind explaining what is wrong with this, hopefully I can learn from it.
You're missing constructors. Add to the public section in header
Invoice() :
description(0), price(0), quantity(0) {}
Invoice(const char* _description, double _price, int _quantity) :
description(_description), price(_price), quantity(_quantity) {}
The error message is pretty clear. You need a constructor for your Invoice
class.
Invoice::Invoice(const char* description_, double price_, int quantity_)
: description(description_), price(price_), quantity(quantity_)
{}
This constructor makes use of initialization list to initialize member variables with constructor arguments.
EDIT: As @the.malkolm pointed you're also making use of Invoice
's default constructor in your code by defining Invoice hammers;
Since you implemented a constructor which takes arugments, you now need need to implement the default constructor as well.
Invoice::Invoice()
: description(0), price(0), quantity(0)
{}
You have not written the constructors
ie. Invoice() { ....} and Invoice(const char * a, double b, int c) {....}
You do not have the correct constructor in your Invoice
class.
精彩评论