Using member var from other class

Started by
4 comments, last by Sir_Spritely 20 years, 7 months ago
What I am trying to do is assign a value to a member variable of another class. I believe the way I am doing it is not the best way plus I am having problems with it. I am getting the following error messages (C++): - Linking... GameGrid.obj : error LNK2001: unresolved external symbol "public: static char Player:lyMark2" (?PlyMark2@Player@@2DA) I have a host of errors exactly like this. Here is what I am trying to do when getting this error: - ** This is in GameGrid.cpp // Place the users position on the game board, in the array. GameBoard[X] [Y] = Player:lyMark2; ** This is in Player.h static char PlyMark2 I have included the header file, the member var I am trying to assign to is static. Is there a better way of accomplishing this. Keep in mind that I am doing the exact same thing with member vars of another 4 classes. Any help appreciated. Paul.
Advertisement
In Player.cpp, add the line:

static char Player:lyMark2; // Declare the static member

Because it is static, it ends up needing to be declared outside the class definition, sorta like member functions (except those can be declared inline)
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Thanks for the tip but it did not work.

Came back with the following: -

C:\Program Files\Microsoft Visual Studio\MyProjects\C++\TicTacToeV1\Player.cpp(28) : error C2720: ''PlyMark2'' : ''static '' storage-class specifier illegal on members

All I changed from the previous post was to add the following line to the top of the code of Player.cpp

static char Player:lyMark2;

Any suggestions?

Paul.
Never mind I resolved it, when declaring it in Player.cpp you do not use the static keyword.

Instead of, static char Player:lyMark2; it is just char Player:lyMark2;.

My next question is, what other more suitable ways are there to resolve such an issue? Or is it simply a case of restructuring all the classes so I don''t run into this problem?

Paul.
Try to minimize static variables as much as possible. Static variable are nothing but glorigied global variable with some restrictions. It is better if you can use private members with a set of inline get/set methods. This will decouple the class and manage memory better.
So if a program is designed effectively then there should never be a need to assign a value to a member variable from class A in class B?

This is what is confusing me right now, Should this example NEVER be allowed to crop up in a program:-

ClassA
{
public:
static int A;
}

ClassB
{
ClassA::A = 5;
}

Dodgy example but you get what I mean. I cannot imagine writing code where one class will never need to use a member var from another class in such a manner.

I was thinking of solving this by either: -

1. Re-designing my code so this problem does not crop up.
2. Bringing in inheritance.
3. Using references.
4. Using pointers.

I would prefer to use either references or pointers to solve such an issue. Would this be considered good practise?

This topic is closed to new replies.

Advertisement