编译器错误:在此上下文中是私有的
compiler error: is private within this context
我正在编写一个类,当我编译时,我收到一条错误消息,说"在此上下文中是私有的",另一条错误消息说"无效使用非静态数据成员"。 但是,如果我注释掉 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"错误是指函数addShipment
、reduceInventory
和getPrice
不是类Product
的成员或朋友,所以它们不能使用它的私有成员。
的无效使用"错误是指您尝试通过使用语法Class::member
限定非静态数据成员来访问非静态数据成员,这是您访问静态数据成员的方式,这些成员由类的所有实例共享。 非静态数据成员使用语法object.member
或pointer->member
访问, 因为每个数据成员的单独实例都存在于类的每个实例中。
我假设你的意思是,如果你注释掉addShipment
函数之后(包括(的所有内容,而不是它之前的所有内容,错误就会消失。这是因为这两种错误都引用非成员函数中的代码。
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 错误"Could not find Boost"(缺少:上下文标头)
- DCMTK 了解"DIMSE 没有有效的演示上下文 ID"错误
- MSVC 在不知道类型的情况下评估上下文(和错误)
- 关于 GetDIBits 和设备上下文的奇怪错误
- 自定义删除器,用于shared_ptr<>给出"无上下文错误"
- Linux/QT-错误创建SSL上下文(错误:140A90C4 ..)
- 分段错误将文本呈现到开罗上下文
- 在spirit parser Action中,在上下文参数上编译boost :: bind(成员函数)上的错误
- OpenGL 上下文创建期间未记录的错误
- 如何修复C#应用程序中调用C++DLL函数时的上下文加载错误
- 带有 QT 错误的 OpenGL:ASSERT:"QOpenGLFunctions::isInitialized(d_ptr)" 。无法创建 OpenGL 上下文
- 当任何子线程导致错误时,上下文切换到主线程
- 带有模板的矢量在打印上下文时在瓦尔格林德中给出错误
- C++上下文错误
- 渲染自定义模型时出现c++Directx 11设备上下文错误
- 此上下文标记中的错误可能会标记错误的位置
- 类声明错误:上下文信息不足,无法确定类型
- 激活上下文生成失败.VC运行时错误
- VS 2013 单元测试不起作用 =设置执行上下文时出现>错误