[英]C++ Default constructor not available
I'm currently learning C++ and reading through "C++ Primer 5th Edition". 我目前正在学习C ++,并正在阅读“ C ++ Primer 5th Edition”。 I just started learning about constructors and I'm having a bit of a problem that I can't figure out.
我刚开始学习有关构造函数的知识,但遇到了一个我无法解决的问题。
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
struct Sales_data
{
//default constructor
Sales_data(const std::string &s, unsigned n, double p):
bookNo(s), units_sold(n), revenue(p*n) { }
//new members: operations on Sales_data objects
std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&);
double avg_price() const;
//data members
std::string bookNo;
unsigned units_sold;
double revenue;
};
I'm pretty sure that the default constructor I wrote is correct (considering it's the one written in the book), but obviously I'm missing something here. 我很确定我编写的默认构造函数是正确的(考虑到这是本书中编写的构造函数),但是显然我在这里遗漏了一些东西。 I don't see any syntax errors or anything and all of the built-in members are being initialized so I have no idea what's wrong.
我看不到任何语法错误或任何东西,并且所有内置成员都正在初始化,所以我不知道出了什么问题。
EDIT : 编辑:
I just found out that it's not my header file giving the error, it's actually my source file. 我只是发现这不是我的头文件给出错误,而是我的源文件。 When I create a Sales_data object like:
当我创建一个Sales_data对象时,例如:
Sales_data total;
it gives me the "No appropriate default constructor available" error. 它给我“没有适当的默认构造函数可用”错误。 I'm still unsure as to what's wrong considering the author gave three ways to write a default constructor, these are them:
考虑到作者给出了三种编写默认构造函数的方法,我仍然不确定这是什么错误:
struct Sales_data {
// constructors added
Sales_data() = default; //Number 1
Sales_data(const std::string &s): bookNo(s) { } //Number 2
Sales_data(const std::string &s, unsigned n, double p): //Number 3
bookNo(s), units_sold(n), revenue(p*n) { }
If those aren't default constructors, then what exactly are they/is their purpose? 如果这些不是默认构造函数,那么它们的用途到底是什么?
A default constructor is a constructor that can be called without passing any argument. 默认构造函数是可以在不传递任何参数的情况下调用的构造函数。
This means that it must take no parameters or that all of them must have a default value. 这意味着它不能包含任何参数,或者所有参数都必须具有默认值。
The default constructor is needed for example when you write 例如,在编写时需要默认构造函数
MyClass x;
because the compiler must be able to generate code to build such an object without any arguments. 因为编译器必须能够生成代码来构建没有任何参数的对象。
Also standard containers may require a default constructor depending on how you use them: for example if you use std::vector::resize
the library can be asked to increase the size of a vector containing your class instances, thus it must be able to create elements without providing any argument. 同样,标准容器可能需要默认构造函数,具体取决于您如何使用它们:例如,如果您使用
std::vector::resize
,则可能要求库增加包含类实例的向量的大小,因此它必须能够创建元素而不提供任何参数。
The problem isn't related to default constructors. 该问题与默认构造函数无关。 The problem indeed is in
Sales_data total;
问题确实出在
Sales_data total;
. 。 What's the book number, sales price and number sold for
total
? 什么是书号,销售价和
total
销量是多少? You must provide them when constructing total
. 构造
total
时必须提供它们。
The constructor of your class takes three parameters, those must be provided when you try to construct an object. 类的构造函数采用三个参数,当您尝试构造一个对象时必须提供这些参数。 Your current variable declaration doesn't provide any parameters:
您当前的变量声明不提供任何参数:
Sales_data total;
Because there are no parameters provided, the compiler tries to use a constructor that doesn't take any parameters. 因为没有提供任何参数,所以编译器会尝试使用不带任何参数的构造函数。 Such a constructor is also called "default constructor".
这样的构造函数也称为“默认构造函数”。 Your class doesn't have a constructor without parameters, so this doesn't work.
您的类没有没有参数的构造函数,因此这是行不通的。
To use the existing constructor, you have to provide the parameters when you create the object: 要使用现有的构造函数,必须在创建对象时提供参数:
Sales_data total("books", 28, 15.99);
Alternatively you could add a constructor to Sales_data
that doesn't take any parameters (a default constructor), and initializes the class with some default values. 或者,您可以向
Sales_data
添加一个不带任何参数的构造函数(默认构造函数),并使用一些默认值初始化该类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.