使用“朋友”不能访问不同类别中的向量。 [英] Cannot reach a vector in a differend class using "Friend"
问题描述
我有2个课程: company
和 cpworker
。
我试图传递一个向量到一个函数 cpworker
,虽然当我尝试到达向量它不让我:
Im trying to pass a vector in company to a function of cpworker
, though when I try to reach the vector it doesn't let me:
在公司
类中:
vector<project> projects;
friend void cpworker::registerWork();
在 cpworker
类中:
void cpworker::registerWork()
{
for (int i = 0; i < company.projects.size(); i++)
{
}
}
正如你所看到的,我试图达到向量的大小,但它不允许我..
此外,我没有使用 get
方法返回公司的项目大小,因为我需要一个完全访问它在循环内。
As you can see, I am trying to reach the size of the vector, but it does not let me..
Also, I didn't use a get
method to company to return the size of the projects, because I need a full access to it inside the loop.
这里是我的错误get(所有在for循环行):
Here are the errors I'm getting (all in the for loop line):
* .projects的左边必须有class / struct / union
* .size的左侧必须有class / struct / union
*不允许类型名称
* left of .projects must have class/struct/union
* left of .size must have class/struct/union
* type name is not allowed
推荐答案
company
是一个类,除非项目是decalred静态的,你不能访问它没有类的实例。要么创建实例,要么使项目成为静态成员。
company
is a class and unless projects is decalred static you can not access it without an instance of the class. Either create an instance or make projects static member.
希望这有帮助。
这篇关于使用“朋友”不能访问不同类别中的向量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!