-1

I have 2 classes: company and 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:

In the company class:

vector<project> projects;
friend void cpworker::registerWork();

In the cpworker class:

void cpworker::registerWork()
{   
    for (int i = 0; i < company.projects.size(); i++)
    {

    }
}

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.

Here are the errors I'm getting (all in the for loop line):

* left of .projects must have class/struct/union
* left of .size must have class/struct/union
* type name is not allowed

David G
  • 94,763
  • 41
  • 167
  • 253
AmitM9S6
  • 131
  • 2
  • 13
  • 2
    From your **incomplete** definition, it seems you are trying to access a member of an instance, without an instance. What is `company` inside `registerWork`? Is it the class name or an instance? what is the class name if it's an instance, than? What is the exact error you got? – StoryTeller - Unslander Monica Dec 27 '12 at 14:41
  • what do you mean by doesn't let me. What is the error you get? – Ivaylo Strandjev Dec 27 '12 at 14:41
  • @izomorphius edited my post at the bottom... @DimaRudnik ` company ` is a class, and `register work` is a method inside a differend class... I dont have any class or anything called `name`... – AmitM9S6 Dec 27 '12 at 14:44
  • 1
    Is `company` in cpworker class a local member variable? From the information provided it is unclear what it is or where it comes from. Your error indicates the compiler has no clue wtf `company` is, and therefore the `.projects` can't find home. – WhozCraig Dec 27 '12 at 14:46
  • May be this answer helps: [Using friend function in C++](http://stackoverflow.com/questions/13884788/using-friend-function-in-c/13885064#13885064) – πάντα ῥεῖ Dec 27 '12 at 14:47
  • Then that's your problem. What is the name of the `company` member variable (or reference, whatever) in `cpworker` class? or is there even one at all?? – WhozCraig Dec 27 '12 at 14:48
  • @WhozCraig Im trying to pass the vector that is called projects into a method in cpworker.. and as I said above, I have both classes... – AmitM9S6 Dec 27 '12 at 14:49
  • I still don't think you're getting it. You're not passing *anything* into `coworker::registerWork()`. You're trying to access a member variable of class `company`, friended or not, *with no instance of the class `company`*. If `projects` is static to `company` then use **`company::projects`** to access it. – WhozCraig Dec 27 '12 at 14:52
  • 1
    Try to define a company class instance like "company theCompany" in cpworker class then try to access the with theCompany.projects – onurozcelik Dec 27 '12 at 14:53
  • I did create an instance and tried to use it, yet it still gives me the same error – AmitM9S6 Dec 27 '12 at 14:56

2 Answers2

3

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.

Hope this helps.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

It's not possible to create member function friend declarations, you can either declare global functions or whole classes as friend, see also: C++ ref, Friendship and inheritance.

In general it's not a good design idea to use friend at all, because it strongly couples the classes together. The better solution will be to couple interfaces (which don't need to be publicly visible anyways). In rare cases it might be a good design decision but that almost always applies to internal details.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190