I guess all of you know this one... static variable in class A acces from B

S

silversurfer2025

Hello everyone,
once again, I have a very basic problem in C++, which I was not able to
solve (maybe because of my Java-Experience or just because it is always
the small syntax-things which break my neck in C++)...

I would like to have a static variable in class A and change its value
from class B (they are not related to each other)


For example:

class A {
poublic:
std::pair <int,int> test
}

class B{
poublic:
void testing();
}

void B::testing() {
std::cout (A::test).first;
A::test = std::make_pair(1,1);
}

If I write it as above, I always get undefined reference to
`A::test'... How should I do it?

Greetings
Tim
 
A

Alf P. Steinbach

* silversurfer2025:
Hello everyone,
once again, I have a very basic problem in C++, which I was not able to
solve (maybe because of my Java-Experience or just because it is always
the small syntax-things which break my neck in C++)...

I would like to have a static variable in class A and change its value
from class B (they are not related to each other)


For example:

class A {
poublic:
std::pair <int,int> test

Missing 'static'. Missing semicolon.

Missing semicolon.

class B{
poublic:
void testing();
}

Missing semicolon.

void B::testing() {
std::cout (A::test).first;

Missing "<<" operator.
A::test = std::make_pair(1,1);
}

If I write it as above, I always get undefined reference to
`A::test'... How should I do it?

Post a minimal and complete program that illustrates the problem.

The above is not the code you have tried.

It is, simply put, just a way to mislead people about what you're
actually trying.
 
S

silversurfer2025

Alf said:
* silversurfer2025:

Missing 'static'. Missing semicolon.


Missing semicolon.
It is just a small example and not a running program..
Missing semicolon.
see above...
Missing "<<" operator.
see above...
Post a minimal and complete program that illustrates the problem.

The above is not the code you have tried.
This is why the code in which I found the problem has more than 1000
lines!
It is, simply put, just a way to mislead people about what you're
actually trying.
Yes, that is exactly what I wanted! Mislead people such that they could
not help me in the end. *rolleyes*

I thought that the problem was so obvious that there would be no
complete program needed, it is not the case that I have a small bug
somewhere in my code, it is a general "how to do this and that"
problem... Sorry if it is not sufficient.

Sorry, but these unnecessary brain-compiler-error messages, which are
pretty obvious btw do not help. My intention was NOT to give a
complete program but to illustrate my problem (even if I must admit
that I totally screw the example by missing the static elements)... If
you just asked for some code which could actually be run it would have
been enough, wouldn't it?

Sorry about that, I just noticed this in many other threads especially
in this group as well that people with small questions get unfriendly
answers with things like "missing semicolon, etc.." without actually
trying to solve the problem... Please remember that people asking
questinons are not little children which should be educated if they did
not ask for it...

Thanks a lot
Tim
 
I

Ian Collins

silversurfer2025 said:
This is why the code in which I found the problem has more than 1000
lines!


Yes, that is exactly what I wanted! Mislead people such that they could
not help me in the end. *rolleyes*

I thought that the problem was so obvious that there would be no
complete program needed, it is not the case that I have a small bug
somewhere in my code, it is a general "how to do this and that"
problem... Sorry if it is not sufficient.

Sorry, but these unnecessary brain-compiler-error messages, which are
pretty obvious btw do not help. My intention was NOT to give a
complete program but to illustrate my problem (even if I must admit
that I totally screw the example by missing the static elements)... If
you just asked for some code which could actually be run it would have
been enough, wouldn't it?
Well if you had posted something that's valid code, it would have
helped. You only have to type it once, everyone else has to read it and
try and fathom out what you are trying to get to work.

All it would have taken is a few more key strokes:

#include <utility>
#include <iostream>

class A {
public:
static std::pair<int,int> test;
};

class B{
public:
void testing();
};

void B::testing() {
std::cout << (A::test).first << std::endl;
A::test = std::make_pair(1,1);
};

Which compiles without error and doesn't exhibit any problems, so we are
still none the wiser as to you problem.
Sorry about that, I just noticed this in many other threads especially
in this group as well that people with small questions get unfriendly
answers with things like "missing semicolon, etc.." without actually
trying to solve the problem... Please remember that people asking
questinons are not little children which should be educated if they did
not ask for it...
Then help us to help you be providing a code snippet that illustrates
your problem. It's common for people to copy what you post and compile
it. If it won't compile, they'll give up and move on.
 
A

Alf P. Steinbach

* silversurfer2025:
Sorry about that, I just noticed this in many other threads especially
in this group as well that people with small questions get unfriendly
answers with things like "missing semicolon, etc.." without actually
trying to solve the problem... Please remember that people asking
questinons are not little children which should be educated if they did
not ask for it...

Please do read the FAQ about how to post (please /do/ that).

Computers are rather literal-minded beasts. They do exactly what
they're told, not what you imagine you're telling them. Some vague
"looks like this, sort of" fantasy example does not illustrate your
problem at all: details do matter, and in programs, extremely much.

Sit down at your computer and create a small example, preferably less
than 20 lines. Chances are you'll solve your problem in the process,
and learn a bit; if you're not willing to invest that much effort, few
people here will be willing to invest any effort explaining things.
Anyway, please post the result here, adult-like. ;-)

Cheers,

- Alf
 
S

silversurfer2025

Alf said:
* silversurfer2025:

Please do read the FAQ about how to post (please /do/ that).
I did.
Computers are rather literal-minded beasts. They do exactly what
they're told, not what you imagine you're telling them. Some vague
"looks like this, sort of" fantasy example does not illustrate your
problem at all: details do matter, and in programs, extremely much.

Sit down at your computer and create a small example, preferably less
than 20 lines. Chances are you'll solve your problem in the process,
and learn a bit; if you're not willing to invest that much effort, few
people here will be willing to invest any effort explaining things.
Again, it is not about being willing to invest that much, it was just
thought as illustration, not as complete working program. I thought it
would be clearer this way..
Anyway, please post the result here, adult-like. ;-)
This is exactly the attitude I was talking about ...

As Ian (thanks a lot Ian, your post really helped and this in a really
nice and explaining way!!) already wrote, the code as such seems to
work fine. In my case (in the program) it seemed to work after I
initialized the element.. However, I am not too sure, because I changed
some lines in the other code as well.

In conclusion I can say that by A::variable I could access static
member variables of other classes without a problem!

Thanks
Tim
 
P

Pavel

silversurfer2025 said:
Hello everyone,
once again, I have a very basic problem in C++, which I was not able to
solve (maybe because of my Java-Experience or just because it is always
the small syntax-things which break my neck in C++)...

I would like to have a static variable in class A and change its value
from class B (they are not related to each other)


For example:

class A {
poublic:
std::pair <int,int> test
}

class B{
poublic:
void testing();
}

void B::testing() {
std::cout (A::test).first;
A::test = std::make_pair(1,1);
}

If I write it as above, I always get undefined reference to
`A::test'... How should I do it?

Greetings
Tim
Just a blind shot: you are getting "undefined reference", not "undefined
variable" or "object". Could it be that you did not include your C++
file with the static member definition for linking?

Some file that contains something like

std::pair<int, int> A::test;

or an object file compiled of such C++ file or a library containing such
object file?
-Pavel
 
S

silversurfer2025

Alf said:
* silversurfer2025:

You won't get more help from me, that's for sure (unless I forget).
To be honest, I do not understand your behaviour at all and this really
is confusing. All I asked for was a little help and your only gave me
compiler errors in response.. I am sorry that this evoked the feeling
that you were not about to help but only to show that you were a
superior (at least this is what I got out of it). If someone who learns
something is asking a question, it might be the best way to help this
person by trying to answer the question and not to only criticise
his/her way of asking... So what do we learn from all this? Does it
really mean that everybode who wants to ask a question has to accept
everything people throw at them because they might otherwise not help
anymore? come on, this can't be it..

Anyway, if I hurt your feelings by telling how I felt I am sorry. I was
simply hurt by your way of treating me in a, as I felt it - it might
have been different, way which was not trying to solve the problem but
just criticising syntax issues..

Sorry,
Tim
 
S

silversurfer2025

Pavel said:
Just a blind shot: you are getting "undefined reference", not "undefined
variable" or "object". Could it be that you did not include your C++
file with the static member definition for linking?

Some file that contains something like

std::pair<int, int> A::test;

or an object file compiled of such C++ file or a library containing such
object file?

Now that you are saying it,... this could have been it as well, I
changed the whole code... Do static variables really HAVE TO be
initialized? If I do not do it it does not work..

Greetings and thanks a lot
Tim
 
I

Ian Collins

silversurfer2025 said:
Now that you are saying it,... this could have been it as well, I
changed the whole code... Do static variables really HAVE TO be
initialized? If I do not do it it does not work..
They don't have to be initialised, but they do have to be defined.
 
S

silversurfer2025

Ian said:
They don't have to be initialised, but they do have to be defined.

OK, maybe I was a little unclear: I have something like this in class
MyClass
static double test;
in my header file.. But this is not enough (i guess) because I have to
write the following on top of my .cpp file, otherwise it does not seem
to work.
int MyClass::test = 0;


Anyway, it is working right now and I am very happy and thank you all
for your help. I promise to post whole compilable code to illustrate my
problems if that is clearly preferred, I just had the impression that
some problems were illustrated easier by code snippets...

the promising-to-better-himself
Tim
 
I

Ian Collins

silversurfer2025 said:
OK, maybe I was a little unclear: I have something like this in class
MyClass
static double test;

That's your declaration.
in my header file.. But this is not enough (i guess) because I have to
write the following on top of my .cpp file, otherwise it does not seem
to work.
int MyClass::test = 0;
That's your definition _and_ initialisation.

Try it without the initialisation if you wish:

int MyClass::test;
 
S

silversurfer2025

Ian said:
That's your declaration.

That's your definition _and_ initialisation.

Try it without the initialisation if you wish:

int MyClass::test;
Aah, ok. Thanks very much!
 
A

Alf P. Steinbach

* silversurfer2025:
To be honest, I do not understand your behaviour at all and this really
is confusing. All I asked for was a little help and your only gave me
compiler errors in response.. I am sorry that this evoked the feeling
that you were not about to help but only to show that you were a
superior (at least this is what I got out of it). If someone who learns
something is asking a question, it might be the best way to help this
person by trying to answer the question and not to only criticise
his/her way of asking... So what do we learn from all this? Does it
really mean that everybode who wants to ask a question has to accept
everything people throw at them because they might otherwise not help
anymore? come on, this can't be it..

Nobody has thrown anything at you; nobody, to my knowledge, has tried to
compile the code you presented; nobody has tried to show off; nobody has
done anything other than to directly help you.

Nobody knows what your technical problem was, although a missing
function definition is a reasonable guess.

Nobody knows if there ever actually /was/ a technical problem.

We don't know because you have refused to describe the problem: there
has been no way to reproduce the alleged problem.

Instead you're focusing on personal things, what someone's motivations
could have been for responding to your call for help.
 
S

silversurfer2025

Alf said:
* silversurfer2025:

Nobody has thrown anything at you; nobody, to my knowledge, has tried to
compile the code you presented; nobody has tried to show off; nobody has
done anything other than to directly help you.

Nobody knows what your technical problem was, although a missing
function definition is a reasonable guess.

Nobody knows if there ever actually /was/ a technical problem.

We don't know because you have refused to describe the problem: there
has been no way to reproduce the alleged problem.

Instead you're focusing on personal things, what someone's motivations
could have been for responding to your call for help.
You are right. I just noticed it very often recently that people asking
questions which are a little more basic are treated in a certain way
(kind of second-class people). Maybe this was why I was a little picky
about your comments because I was afraid that this might happen to me
as well...

As I already said, I am sorry for being so picky,.. it did not help
anybody.
 
S

silversurfer2025

One more thing ;)
We don't know because you have refused to describe the problem: there
has been no way to reproduce the alleged problem.
I did not _refuse_ to describe the problem, I guess. I described it as
good as I could. the only thing missing was a compilable version of it
(and as written before I did not think it was necessary)...

But let's stop this hick-hack now, please? I thank you all for giving
me help, even if I could not appreciate some of it in the first place...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top