473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
16 1444
* 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

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
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
* 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

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
* 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
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

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

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
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

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
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

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
* 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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Anthony | last post by:
Hi, Can anyone help me out here? I need a construction where a static variable in a derived class is accessible through its base class. The base class is part of a framework. The variable in...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
6
by: Nils | last post by:
Hi, I want to do a vector, that is the same in every template class, is it possible? Example: .... class temp<int>.... static vector A; class temp<float>... static vector A; Now I want to...
5
by: SemSem | last post by:
when i was usong c++ idont have to put static before tha function that i want to acces but in c# i have to but it . ex : ihave a function called add(int x,int y) i have to put static int...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.