C++ 编译器错误:在此上下文中是私有的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43154155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
compiler error: is private within this context
提问by AdamK
I'm writing a class and when I compile, I get one error message that says, "is private within this context" and another that says, "invalid use of non-static data member". But if I comment out everything before the addShipment function in my cpp file, it compiles just fine. Can someone please explain to me what is different to sometimes cause an error and sometimes not, and if the two different types of errors are related.
我正在编写一个类,当我编译时,我收到一条错误消息,指出“在此上下文中是私有的”,而另一条则指出“非静态数据成员的使用无效”。但是如果我在我的 cpp 文件中的 addShipment 函数之前注释掉所有内容,它编译就好了。有人可以向我解释有时会导致错误而有时不会导致错误的不同之处,以及两种不同类型的错误是否相关。
Product.h:
产品.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:
产品.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;
}
采纳答案by Fatih BAKIR
There are mainly 2 problems in your code, first, you are confusing static and non-static member variables and second, your last 3 functions are declared in the class declaration but you didn't qualify them with Product::
so they are free standing functions with the same names. If you intended them to be the definitions of the members, qualify them.
您的代码中主要有 2 个问题,首先,您混淆了静态和非静态成员变量,其次,您的最后 3 个函数是在类声明中声明的,但您没有限定它们,Product::
因此它们是独立的函数相同的名字。如果您打算将它们作为成员的定义,请对它们进行限定。
To fix the first point, when you have a class like the following:
要解决第一点,当您有如下类时:
struct some_class
{
int nonstatic_member;
static int static_member;
}
You access the static ones using the TypeName::MemberName
syntax:
您可以使用以下TypeName::MemberName
语法访问静态的:
some_class::static_member = 5;
However, when a member is not static, you need an instance of that class to access the member:
但是,当成员不是静态成员时,您需要该类的一个实例来访问该成员:
some_class some_object;
some_object.nonstatic_member = 10;
The same rules apply for member functions as well:
相同的规则也适用于成员函数:
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
}
Since none of your member variables are static, your use of Product::
everywhere is the cause of the second error.
由于您的所有成员变量都不是静态的,因此您使用了Product::
Everywhere 是导致第二个错误的原因。
If you're not sure about the static memberconcept, here is a read: http://en.cppreference.com/w/cpp/language/static
如果您不确定静态成员的概念,请阅读以下内容:http: //en.cppreference.com/w/cpp/language/static
回答by Joseph Thomson
The "private within this context" error refers to the fact that the functions addShipment
, reduceInventory
and getPrice
are not members or friends of the class Product
, so they cannot use its private members.
“此上下文中的私有”错误是指函数addShipment
,reduceInventory
和getPrice
不是类的成员或朋友Product
,因此它们不能使用其私有成员。
The "invalid use of non-static data member" error refers to the fact that you are trying to access non-static data members by qualifying them using the syntax Class::member
, which is how you access staticdata members, which are shared by all instances of a class. Non-static data members are accessed using the syntax object.member
or pointer->member
, as separate instances of each data member exist for each instance of a class.
“非静态数据成员的无效使用”错误是指您试图通过使用语法限定非静态数据成员来访问非静态数据成员Class::member
,这就是您访问静态数据成员的方式,静态数据成员由所有实例共享一类。使用语法object.member
or访问非静态数据成员pointer->member
,因为每个类的每个实例都存在每个数据成员的单独实例。
And I assume that you mean the errors disappear if you comment out everything after(and including) the addShipment
function, rather than everything before it. This is because both kinds of error refer to code within the non-member functions.
而且我假设您的意思是如果您注释掉函数之后(并包括)的addShipment
所有内容,而不是它之前的所有内容,错误就会消失。这是因为这两种错误都涉及非成员函数中的代码。