C++;使用代码块初始化 我通过C++入门书学习C++。它是关于用一些对象创建一个类。我真的不知道该怎么做才能让程序正常运行。我正在使用代码块13.12,我复制了以下代码: #include <iostream> #include "Sales_item.h" int main() { Sales_item book; //read ISBN, number of copies sold, and sales price std::cin>>book; //write ISBN, number of copies sold, total revenue, and average price std::cout<<book<<std:endl; return 0; }

C++;使用代码块初始化 我通过C++入门书学习C++。它是关于用一些对象创建一个类。我真的不知道该怎么做才能让程序正常运行。我正在使用代码块13.12,我复制了以下代码: #include <iostream> #include "Sales_item.h" int main() { Sales_item book; //read ISBN, number of copies sold, and sales price std::cin>>book; //write ISBN, number of copies sold, total revenue, and average price std::cout<<book<<std:endl; return 0; },c++,C++,第二个: Sales_item.h.cpp #include "Sales_item.h.h" Sales_item.h::Sales_item.h() { //ctor } Sales_item.h::~Sales_item.h() { //dtor } 作者要求将以下代码复制到当前目录。然而,我不知道他所说的“当前目录”是什么意思 /*此文件定义了第1章中使用的销售项目类。 *本文件中使用的代码将在中解释 *第7章(类)和第14章(重载运算符) *读者不应试图理解此文

第二个:

Sales_item.h.cpp

#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
    //ctor
}

Sales_item.h::~Sales_item.h()
{
    //dtor
}
作者要求将以下代码复制到当前目录。然而,我不知道他所说的“当前目录”是什么意思

/*此文件定义了第1章中使用的销售项目类。
*本文件中使用的代码将在中解释
*第7章(类)和第14章(重载运算符)
*读者不应试图理解此文件中的代码
*直到他们读完那些章节。
*/
#ifndef销售项目
//只有在SALESITEM_H尚未定义的情况下,我们才会出现在这里
#定义销售项目
//此处定义了Sales_item类和相关函数
#包括
#包括
类别销售商品{
//第270页第7.2.1节解释了这些声明
//第14章第557、558、561页
friend std::istream&operator>>(std::istream&销售项目&);
friend std::ostream&operator作者已“询问”您需要将代码复制到
Sales\u item.h
文件中。请删除您自己的类
Sales\u item.h
,包括两个文件
Sales\u item.h.h
Sales\u item.h.cpp
,然后重新开始编写作者的
Sales\u item.h
文件,并根据需要创建
Sales\u item.cpp

类名是
Sales\u item
,而不是
Sales\u item.h

当前目录意味着你的代码驻留在哪里。

在一个文件中编写所有代码通常是非常麻烦的。因此,C++允许你在不同的文件中破解代码,然后将其包含在需要的文件中。
#include“Sales_item.h”
只是告诉编译器在当前程序中包含Sales_item.h中的所有代码。
出现错误的原因是您的计算机中没有Sales_item.h文件

您可以获取Sales_item.h和本书可能需要的其他资源文件 .
将Sales_item.h复制到与代码相同的目录中


<> P.S.在你复制资源之前试着自己写例子。

你得到了什么错误?顶部的星号是否是一个错字?如果你在使用一本书,确保你读得很彻底,剪切和粘贴不是一般学习的最好方法。这似乎更像是一个比C++问题更为复杂的问题。你为什么要命名C?lass“Sales_item.h”?您好,谢谢!我已经创建了Sales_item.h文件并将代码复制到该文件中。但是在第10行(std::cin>>book;)和第12行(std::cout@user3689783看起来您需要实现
操作符>>()
操作符
Sales_item.h.cpp

#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
    //ctor
}

Sales_item.h::~Sales_item.h()
{
    //dtor
}
/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
    // these declarations are explained section 7.2.1, p. 270
    // and in chapter 14, pages 557, 558, 561
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
    friend bool operator<(const Sales_item&, const Sales_item&);
    friend bool operator==(const Sales_item&, const Sales_item&);
public:
    // constructors are explained in section 7.1.4, pages 262 - 265
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
    Sales_item(const std::string &book):
    bookNo(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);

    // operations on Sales_item objects
    std::string isbn() const { return bookNo; }
    double avg_price() const;
// private members as before
private:
    std::string bookNo; // implicitly initialized to the empty string
    unsigned units_sold;
    double revenue;
};