2017-04-01 36 views
2

我正在写一个类,当我编译,我得到的说,“是在这一范围内的私人”,另一个说,“无效使用非静态数据的一个错误消息中的私人会员”。但是,如果我在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; 
} 
+1

无偿列入'',还没有包括''? –

回答

1

你的代码中主要存在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; 

在S AME规则适用于成员函数还有:

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

1

“此范围内的私人” 的错误指的是一个事实,即功能addShipmentreduceInventorygetPrice不是会员或类Product的朋友,所以他们不能使用它的私有成员。

“无效使用非静态数据成员的”错误指的是事实,你试图访问使用语法Class::member,这是你如何访问静态数据成员进行限定的非静态数据成员,这是由一个类的所有实例共享的。非静态数据成员所使用的语法或object.member访问pointer->member,作为一类的每个实例存在的每个数据成员的单独实例。

而且我认为你的意思是,如果你之前后注释掉一切(含)的addShipment功能,而不是一切错误消失。这是因为这两种错误都涉及到非成员函数中的代码。