我一直有一個問題,當我嘗試編譯時給我一個錯誤。錯誤說,error: no matching function for call to 'Invoice::Invoice(const char [10], double, int)'
這是給我的C++編譯問題
//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;
}
我會假設我在管道部分擰了一些東西。你必須記住,這是我第一次使用C++。所以如果你不介意解釋這個有什麼問題,希望我可以從中吸取教訓。
這是一個從一前一後跟進? –
@Ed:[這是一個轉貼](http://stackoverflow.com/questions/7513261/c-issue-with-program-not-compiling) –
@比利:跆拳道你在做什麼?不要把你的問題的內容替換爲「它有效!它有效!它有效!」請。 –