compiler error: is private within this context

compiler error: is private within this context

我正在编写一个 class,当我编译时,我收到一条错误消息说 "is private within this context" 和另一条说 "invalid use of non-static data member"。但是,如果我在我的 cpp 文件中注释掉 addShipment 函数之前的所有内容,它就可以正常编译。有人可以向我解释一下有时会导致错误而有时不会导致错误的不同之处,以及这两种不同类型的错误是否相关。

Product.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

Product.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;
}

"private within this context" 错误是指函数 addShipmentreduceInventorygetPrice 不是 class [= 的成员或友元13=],所以他们不能使用它的私有成员。

"invalid use of non-static data member" 错误是指您试图通过使用语法 Class::member 限定非静态数据成员来访问它们,这就是您访问 static 的方式 个数据成员,由 class 的所有实例共享。使用语法 object.memberpointer->member 访问非静态数据成员,因为每个数据成员的单独实例存在于 class.

的每个实例中

我假设您的意思是,如果您注释掉 (包括)addShipment 函数之后的所有内容,而不是它之前的所有内容,错误就会消失。这是因为这两种错误都引用了非成员函数中的代码。

你的代码主要有两个问题,首先,你混淆了静态和非静态成员变量,其次,你的最后 3 个函数在 class 声明中声明,但你没有限定它们使用 Product:: 所以它们是具有相同名称的独立函数。如果您希望它们成为成员的定义,请对它们进行限定。

为了修正第一点,当你有一个像下面这样的 class 时:

struct some_class
{
    int nonstatic_member;
    static int static_member;
}

您使用 TypeName::MemberName 语法访问静态的:

some_class::static_member = 5;

但是,当成员不是静态成员时,您需要该成员的实例 class 才能访问该成员:

some_class some_object;
some_object.nonstatic_member = 10;

同样的规则也适用于成员函数:

void some_class::some_function()
{
    some_class::nonstatic_member = 10; // error
    this->nonstatic_member = 10; // fine
    nonstatic_member = 10; // fine

    static_member = 5; // fine
    some_class::static_member = 5; // fine
}

因为你的 none 成员变量是静态的,你到处使用 Product:: 是第二个错误的原因。

如果您不确定 静态成员 概念,请阅读:http://en.cppreference.com/w/cpp/language/static