编译器错误:在此上下文中是私有的

compiler error: is private within this context

本文关键字:上下文 错误 编译器      更新时间:2023-10-16

我正在编写一个类,当我编译时,我收到一条错误消息,说"在此上下文中是私有的",另一条错误消息说"无效使用非静态数据成员"。 但是,如果我注释掉 cpp 文件中 addShipping 函数之前的所有内容,它可以很好地编译。 有人可以向我解释一下有时导致错误有时不会导致错误的不同之处,以及两种不同类型的错误是否相关。

产品.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;
}

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

要修复第一点,当您有如下所示的类时:

struct some_class
{
    int nonstatic_member;
    static int static_member;
}

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

some_class::static_member = 5;

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

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
}

由于所有成员变量都不是静态的,因此在任何地方使用 Product:: 是导致第二个错误的原因。

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

"private in this context"错误是指函数addShipmentreduceInventorygetPrice不是类Product的成员或朋友,所以它们不能使用它的私有成员。

"非静态数据成员

的无效使用"错误是指您尝试通过使用语法Class::member限定非静态数据成员来访问非静态数据成员,这是您访问静态数据成员的方式,这些成员由类的所有实例共享。 非静态数据成员使用语法object.memberpointer->member访问, 因为每个数据成员的单独实例都存在于类的每个实例中。

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