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

时间:2017-04-01 07:39:31

标签: c++ class private

我正在编写一个类,当我编译时,我收到一条错误消息,上面写着"在此上下文中是私有的"另一个说,"无效使用非静态数据成员"。但是如果我在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;
}

2 个答案:

答案 0 :(得分:3)

&#34;在此背景下的私人&#34;错误是指函数addShipmentreduceInventorygetPrice不是类Product的成员或朋友,因此他们无法使用其私有成员。

&#34;无效使用非静态数据成员&#34;错误是指您尝试通过使用语法Class::member对其进行限定来访问非静态数据成员,这是您访问由所有实例共享的静态数据成员的方式一堂课。使用语法object.memberpointer->member访问非静态数据成员,因为每个类的实例都存在每个数据成员的单独实例。

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

答案 1 :(得分:3)

您的代码中主要存在两个问题,第一,您混淆静态和非静态成员变量;第二,您的最后3个函数在类声明中声明,但您没有用{{1}来限定它们所以它们是具有相同名称的独立功能。如果您打算将它们作为成员的定义,请对其进行限定。

要修复第一点,当您拥有类似以下的类时:

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 } 都是导致第二次错误的原因。

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