【发布时间】:2017-08-26 12:29:58
【问题描述】:
我正在编写一个类,当我编译时,我收到一条错误消息,上面写着“在此上下文中是私有的”,另一个错误消息是“无效使用非静态数据成员”。但是,如果我在我的 cpp 文件中注释掉 addShipment 函数之前的所有内容,它就可以编译得很好。有人可以向我解释一下有时会导致错误,有时不会,以及这两种不同类型的错误是否相关。
产品.h:
#ifndef PRODUCT_H
#define PRODUCT_H
#include <iostream>
class Product {
public:
Product(int productID, std::string productName);
std::string getDescription();
void setDescription(std::string description);
std::string getName();
int getID();
int getNumberSold();
double getTotalPaid();
int getInventoryCount();
void addShipment(int shipmentQuantity, double shipmentCost);
void reduceInventory(int purchaseQuantity);
double getPrice();
private:
int productID;
std::string name;
std::string description;
double totalPaid;
int inventoryCount;
int numSold;
};
#endif
产品.cpp:
#include <iostream>
#include "Product.h"
using namespace std;
Product::Product(int productID, string productName) {
Product::productID = productID;
Product::name = productName;
}
string Product::getDescription() {
return Product::description;
}
void Product::setDescription(string description) {
Product::description = description;
}
string Product::getName() {
return Product::name;
}
int Product::getID() {
return Product::productID;
}
int Product::getNumberSold() {
return Product::numSold;
}
double Product::getTotalPaid() {
if(Product::totalPaid < 1) {
totalPaid = 0;
}
return Product::totalPaid;
}
int Product::getInventoryCount() {
Product::inventoryCount = 0;
return Product::inventoryCount;
}
void addShipment(int shipmentQuantity, double shipmentCost) {
Product::inventoryCount = Product::inventoryCount + shipmentQuantity;
Product::totalPaid = Product::totalPaid + shipmentCost;
}
void reduceInventory(int purchaseQuantity) {
Product::inventoryCount = Product::inventoryCount - purchaseQuantity;
Product::numSold = Product::numSold + purchaseQuantity;
}
double getPrice() {
int price = (Product::totalPaid / static_cast<double>(Product::inventoryCount + Product::numSold)) * 1.25;
return price;
}
【问题讨论】:
-
无偿包含
<iostream>,但不包含<string>?