By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,930 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,930 IT Pros & Developers. It's quick & easy.

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

P: n/a
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,inttest
}

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

Jul 22 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
* 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,inttest
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '06 #2

P: n/a

Alf P. Steinbach schrieb:
* 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,inttest

Missing 'static'. Missing semicolon.
}

Missing semicolon.
It is just a small example and not a running program..
>
class B{
poublic:
void testing();
}

Missing semicolon.
see above...
>
void B::testing() {
std::cout (A::test).first;

Missing "<<" operator.
see above...
>
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.
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

Jul 22 '06 #3

P: n/a
silversurfer2025 wrote:
Alf P. Steinbach schrieb:
>>
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?
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,inttest;
};

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.

--
Ian Collins.
Jul 22 '06 #4

P: n/a
* 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '06 #5

P: n/a

Alf P. Steinbach schrieb:
* 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).
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
>
Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '06 #6

P: n/a
* silversurfer2025:
This is exactly the attitude I was talking about ...
You won't get more help from me, that's for sure (unless I forget).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '06 #7

P: n/a
silversurfer2025 wrote:
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,inttest
}

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, intA::test;

or an object file compiled of such C++ file or a library containing such
object file?
-Pavel
Jul 22 '06 #8

P: n/a

Alf P. Steinbach wrote:
* silversurfer2025:
This is exactly the attitude I was talking about ...

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

>
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '06 #9

P: n/a

Pavel wrote:
silversurfer2025 wrote:
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,inttest
}

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, intA::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
-Pavel
Jul 23 '06 #10

P: n/a
silversurfer2025 wrote:
>>
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, intA::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..
They don't have to be initialised, but they do have to be defined.

--
Ian Collins.
Jul 23 '06 #11

P: n/a

Ian Collins wrote:
silversurfer2025 wrote:
>
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, intA::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..
They don't have to be initialised, but they do have to be defined.

--
Ian Collins.
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

Jul 23 '06 #12

P: n/a
silversurfer2025 wrote:
Ian Collins wrote:
>>silversurfer2025 wrote:
>>>>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, intA::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..

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

--
Ian Collins.


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;

--
Ian Collins.
Jul 23 '06 #13

P: n/a

Ian Collins wrote:
silversurfer2025 wrote:
Ian Collins wrote:
>silversurfer2025 wrote:

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, intA::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..
They don't have to be initialised, but they do have to be defined.

--
Ian Collins.

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;
Aah, ok. Thanks very much!
--
Ian Collins.
Jul 23 '06 #14

P: n/a
* silversurfer2025:
Alf P. Steinbach wrote:
>* silversurfer2025:
>>This is exactly the attitude I was talking about ...
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..
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '06 #15

P: n/a

Alf P. Steinbach schrieb:
* silversurfer2025:
Alf P. Steinbach wrote:
* silversurfer2025:
This is exactly the attitude I was talking about ...
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..

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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 24 '06 #16

P: n/a
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...

Jul 24 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.