c++编译问题
C++ compiling Issue
当我尝试编译时,我一直有一个错误给我的问题。错误显示为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);
这是我的代码,第一个你需要把它保存为发票。它花了我一段时间来实际修复大多数错误。这是我唯一的错误。
#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;
}
我想我把ductape部分搞砸了。你必须记住,这是我第一次学c++。所以,如果你不介意解释一下这个错误,希望我能从中吸取教训。
您缺少构造函数。添加到header
中的公共部分Invoice() :
description(0), price(0), quantity(0) {}
Invoice(const char* _description, double _price, int _quantity) :
description(_description), price(_price), quantity(_quantity) {}
错误消息非常清楚。你需要一个Invoice
类的构造函数。
Invoice::Invoice(const char* description_, double price_, int quantity_)
: description(description_), price(price_), quantity(quantity_)
{}
这个构造函数使用初始化列表来初始化带有构造函数参数的成员变量。
编辑:作为@the。malkolm指出,通过定义Invoice hammers;
Invoice
的默认构造函数既然实现了一个接受参数的构造函数,那么现在还需要实现默认构造函数。
Invoice::Invoice()
: description(0), price(0), quantity(0)
{}
您没有编写构造函数
ie. Invoice() { ....} and Invoice(const char * a, double b, int c) {....}
您的Invoice
类中没有正确的构造函数
相关文章:
- 编译包含字符串的代码时遇到问题
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 如何在 Mac 上正确编译C++,当它在 Linux 上编译没有问题时?
- Rextester 语言编译C++问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- C++ 多级虚拟继承编译问题
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- Qt Visual Studio 2015 加载项编译问题
- 构造函数和 G++ 编译配方的问题
- 解析问题 - 预期的非限定 ID - #include <array> 编译错误
- libssh 问题编译问题未定义的错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- Visual Studio 2017问题编译了C 代码,而该代码则在Linux中编译
- 这个C++代码有什么问题?编译错误:调用“测试::测试(测试)”没有匹配函数
- 试图编译基本Boost PropertyTree示例时出现问题-编译错误.该怎么做
- 如何解决***缺少分隔符的问题.编译QT(或支持库)时