我正在编写一个类,当我编译时,我收到一条错误消息,指出“在此上下文中是私有的”,而另一条则指出“非静态数据成员的使用无效”。但是如果我在我的 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
Run Code Online (Sandbox Code Playgroud)
产品.cpp:
#include <iostream>
#include "Product.h"
using namespace std;
Product::Product(int productID, string productName) {
Product::productID = productID;
Product::name = productName;
} …
Run Code Online (Sandbox Code Playgroud)