Problem:
A.cpp:
------
static FOO* gFoo=NULL;
A.h
extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like:
extern static FOO* gFoo;
it gives:
C2159: more than one storage class specified.
<blink, blink>
How would I do this now? I have a prefedined macro that makes a
"static FOO*". And I want to tell all my other .cpp files that it
exists in stdafx.h.
Thank you,
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do www.GLBasic.com 10 28323
Try this:
A.h
static FOO* gFoo;
A.cpp:
extern static FOO* gFoo=NULL;
Vince
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2k*************@uni-berlin.de... Problem:
A.cpp: ------ static FOO* gFoo=NULL;
A.h extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like: extern static FOO* gFoo;
it gives: C2159: more than one storage class specified.
<blink, blink> How would I do this now? I have a prefedined macro that makes a "static FOO*". And I want to tell all my other .cpp files that it exists in stdafx.h.
Thank you,
-- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
________________________________________ Looking for a good game? Do it yourself! GLBasic - you can do www.GLBasic.com
Gernot Frisch wrote: Problem:
A.cpp: ------ static FOO* gFoo=NULL;
A.h extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
if A.h looks like: extern static FOO* gFoo;
it gives: C2159: more than one storage class specified.
Yes, the variable gFoo cannot be static and extern at the same time.
<blink, blink> How would I do this now? I have a prefedined macro that makes a "static FOO*". And I want to tell all my other .cpp files that it exists in stdafx.h.
Thank you,
Solution:
A.cpp:
------
FOO* gFoo=NULL; // Note no static
A.h
----
extern FOO* gFoo;
HTH
Mark
> Solution: A.cpp: ------ FOO* gFoo=NULL; // Note no static
A.h ---- extern FOO* gFoo;
I rewrote the code so I can do this now. But I don't understand it.
Why can't I tell another .cpp file that there is an static variable
somewhere else?
-Gernot
"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2k*************@uni-berlin.de... Solution:
I rewrote the code so I can do this now. But I don't understand it. Why can't I tell another .cpp file that there is an static variable somewhere else?
Because static variables having namespace scope have internal linkage i.e.
the entity it denotes can be referred to by names from other scopes in the
same translation unit only.
Gernot Frisch wrote: Problem:
A.cpp: ------ static FOO* gFoo=NULL;
A.h extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
Well, static means internal linkage, extern means external linkage. You
have to choose one. If the variable is static, it doesn't make much
sense to declare it as extern in the header.
if A.h looks like: extern static FOO* gFoo;
it gives: C2159: more than one storage class specified.
<blink, blink> How would I do this now? I have a prefedined macro that makes a "static FOO*". And I want to tell all my other .cpp files that it exists in stdafx.h.
Why? It's static, so it doesn't exist outside of the .cpp file where
it's defined.
> Why? It's static, so it doesn't exist outside of the .cpp file where it's defined.
Because: That is exactly what I didn't know. :) Thank you,
Gernot
Gernot Frisch wrote: ... Why can't I tell another .cpp file that there is an static variable somewhere else?
Because that what this particular use of 'static' is intended to do in
the first place! It is intended to make the variable "invisible" from
other translation units. If you want to be able to link to that variable
from other translation units - give it external linkage.
--
Best regards,
Andrey Tarasevich
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cb*************@news.t-online.com... Gernot Frisch wrote:
Problem:
A.cpp: ------ static FOO* gFoo=NULL;
A.h extern FOO* gFoo;
gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
Well, static means internal linkage, extern means external linkage. You have to choose one. If the variable is static, it doesn't make much sense to declare it as extern in the header.
<rant>
You know, I can't for the life of me figure out why "static" was used to
mean internal linkage in this case. I'd think "intern" or something similar
would be much more logical, and simply eliminate the use of "static" for
non-member functions and variables. I know this is not the forum for asking
"why", but it really bugs me to have a keyword that has no apparent relation
to its use. One would think that "static" should have the same (or at least
similar) meaning in this context as it does in member functions and
variables. And since it makes no sense to try to apply the same meaning in
this case as in the member (or local variable) case, it really just
shouldn't exist at all here. "But that's just my opinion...I could be
wrong."
</rant>
-Howard
Howard wrote: Well, static means internal linkage, extern means external linkage. You have to choose one. If the variable is static, it doesn't make much sense to declare it as extern in the header.
<rant>
You know, I can't for the life of me figure out why "static" was used to mean internal linkage in this case. I'd think "intern" or something similar would be much more logical, and simply eliminate the use of "static" for non-member functions and variables.
I think they didn't want to introduce a new keyword. Also, they wanted
to remain compatible to C, which alread had defined it this way. And
actionally the meaning that static has within a class definition is the
youngest one.
I know this is not the forum for asking "why", but it really bugs me to have a keyword that has no apparent relation to its use.
I had more problems with the syntax they chose for pure virtual
functions. "Initializing" a function declaration with 0 looks very odd
to me. But again, I suspect they didn't want to add a new keyword
"pure".
One would think that "static" should have the same (or at least similar) meaning in this context as it does in member functions and variables.
It depends on how you look at it. If you look at it the right way,
static has more or less always the same meaning.
Within a class, static means that the function or variable is only there
once, for the class, not for each instance of it. Same for a local
static variable, if you count calling a function as "instantiating" it.
Well, and a static variable/function on namespace scope is there for
this one translation unit. It always has a meaning similar to "there is
only one". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Joe |
last post by:
I have 2 cpp modules (test.cpp, main.cpp). There is a static int
record_count variable declared globally in the test.cpp and incremented
in a function contained in the test.cpp module.
I would...
|
by: Mark A. Gibbs |
last post by:
I have a question about mixing C and C++.
In a C++ translation unit, I want to define a function with internal
linkage and C calling convention. Here's a sample of what I want to do:
//...
|
by: Fredrik Olsson |
last post by:
Hello I am developing a cross-platform solution for scheduled messages,
primary for SMS. The server parts are written in ASNI/ISO C to compile and
run cleanly on any POSIX compatible server.
For...
|
by: ss |
last post by:
hi!
i am trying to use
private static extern int PlaySound(String pszSound, int falgs);
but it doesn't play the sound although it exists (even by checking
file.exist)
any solutions?
10x
|
by: Sean |
last post by:
I am a little confused by the "extern inline and static inline" rules. I
understand that "extern inline" guarantees no function storage is
created (all are inlined). But the following test seems to...
|
by: Christian Christmann |
last post by:
Hi,
I've tree questions on the storage class specifier "extern":
1) Code example:
int main( void )
{
int b = -2; // my line 3
if ( a ) {
|
by: anuragch1 |
last post by:
#include <stdio.h>
void main()
{
static int i = 1;
void cal();
printf("%d\n",i);
cal();
}
void cal()
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared...
|
by: Dinesh143 |
last post by:
Where it is store the variables auto,static,extern,register,typedef,_declspec?
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
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...
| |