Archived

This topic is now archived and is closed to further replies.

Sir_Spritely

Using member var from other class

Recommended Posts

Sir_Spritely    122
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.

Share this post


Link to post
Share on other sites
Wyrframe    2426
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)

Share this post


Link to post
Share on other sites
Sir_Spritely    122
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.

Share this post


Link to post
Share on other sites
Sir_Spritely    122
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.

Share this post


Link to post
Share on other sites
arunvb    147
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.

Share this post


Link to post
Share on other sites
Sir_Spritely    122
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?

Share this post


Link to post
Share on other sites