【问题标题】:compiler error: is private within this context编译器错误:在此上下文中是私有的
【发布时间】: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;
}

【问题讨论】:

  • 无偿包含&lt;iostream&gt;,但不包含&lt;string&gt;

标签: c++ class private


【解决方案1】:

“此上下文中的私有”错误是指函数addShipmentreduceInventorygetPrice 不是类Product 的成员或朋友,因此它们不能使用其私有成员。

“无效使用非静态数据成员”错误是指您试图通过使用语法Class::member 限定非静态数据成员来访问它们,这就是您访问静态 数据成员,由类的所有实例共享。使用语法 object.memberpointer-&gt;member 访问非静态数据成员,因为每个数据成员的单独实例存在于类的每个实例中。

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

【讨论】:

    【解决方案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

    【讨论】:

      猜你喜欢
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2017-09-04
      • 2013-04-27
      • 2015-10-02
      • 1970-01-01
      相关资源
      最近更新 更多