2011-09-22 129 views
-4

我一直有一個問題,當我嘗試編譯時給我一個錯誤。錯誤說,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++。所以如果你不介意解釋這個有什麼問題,希望我可以從中吸取教訓。

+0

這是一個從一前一後跟進? –

+1

@Ed:[這是一個轉貼](http://stackoverflow.com/questions/7513261/c-issue-with-program-not-compiling) –

+2

@比利:跆拳道你在做什麼?不要把你的問題的內容替換爲「它有效!它有效!它有效!」請。 –

回答

4

錯誤信息非常清晰。你需要一個你的Invoice類的構造函數。

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

此構造利用初始化列表中初始化成員變量與構造函數參數。

編輯:由於@ the.malkolm指出你還通過定義Invoice hammers;

利用在你的代碼Invoice的默認構造函數既然你實現一個構造函數,需要arugments,你現在需要需要實現也是默認的構造函數。

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

是的,但不要忘記非參數的一個。作者也使用它。 –

+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

你不必在你的Invoice類的構造方法正確。

+0

'ductTape'也不包含任何參數。 –

+0

正確,謝謝。 –

1

您還沒有書面的建設者

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