KCrookedHand KCrookedHand - 1 month ago 6
C++ Question

Illegal reference to non-static member in 2 classes

I have problem with my class/pointers.
I have two classes

FirstClass
and
SecondClass
.
FirstClass
has two pointers:

MyClass *character1;
MyClass *character2;


I assign to these pointers later in my code but now i have my
SecondClass
where i also have 2 pointers:

MyClass *oldChar1;
MyClass *oldChar2;


I want to set
oldChar
to the same as indicated by
character
. I made a function in
SecondClass
with friend clause in
FirstClass
.

void SecondClass::setChars()
{
*oldChar1 = FirstClass::character1;
*oldChar2 = FirstClass::character2;
}


Result:
illegal reference to non-static member 'FirstClass::character1'

I dont get it :/ Somebody can help me??

Answer

There are a few approaches to do this.

A friend function shared in both classes (source).

The friend function has access to the data members of the class MyFirstClass and MySecondClass, but the friend function still needs to be told which instances of those classes to use (see this) e.g.

class MyClass {

};

class MySecondClass; //forward declaration for later

class MyFirstClass {

private:
  MyClass *character1;
  MyClass *character2;

public:
  //friend function
  friend void setChars(MyFirstClass& c1, MySecondClass& c2); //<---- friend function

};

class MySecondClass {

private:
  MyClass *oldChar1;
  MyClass *oldChar2;

public:
  //friend function
  friend void setChars(MyFirstClass& c1, MySecondClass& c2);

};

//define the friend function
void setChars(MyFirstClass& c1, MySecondClass& c2) {
  c2.oldChar1 = c1.character1;
  c2.oldChar2 = c1.character2;
}

int main() {

  MyFirstClass c1;
  MySecondClass c2;

  setChars(c1,c2);

}

Friend Class. Make MySecondClass a friend of MyFirstClass. MySecondClass will have method setChars which will take an instance of MyFirstClass as a parameter. e.g.

class MyClass {

};

class MySecondClass;

class MyFirstClass {

private:
  MyClass *character1;
  MyClass *character2;

public:   
  friend MySecondClass; //<----- friend class

};

class MySecondClass {

private:
  MyClass *oldChar1;
  MyClass *oldChar2;

public:

  void setCharsUsingFriendClass(MyFirstClass& c1) {
    oldChar1 = c1.character1;
    oldChar2 = c1.character2;
  }


};

int main() {

  MyFirstClass c1;
  MySecondClass c2;

  c2.setCharsUsingFriendClass(c1);

}

And finally using 2 getter methods. I think this approach is better because it allows the classes to hide their implementation details (encapsulation).

class MyClass {

};

class MySecondClass;

class MyFirstClass {

private:
  MyClass *character1;
  MyClass *character2;

public:

  //define getters to access the private members
  MyClass* GetCharacter1() {return character1;}
  MyClass* GetCharacter2() {return character2;}
};

class MySecondClass {

private:
  MyClass *oldChar1;
  MyClass *oldChar2;

public:

  void setCharsUsingGetters(MyFirstClass& c1) {
    oldChar1 = c1.GetCharacter1();
    oldChar2 = c1.GetCharacter2();
  }
};

int main() {

  MyFirstClass c1;
  MySecondClass c2;

 c2.setCharsUsingGetters(c1);

}