Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extern storage class specified

713 views
Skip to first unread message

ghada glissa

unread,
May 22, 2015, 6:16:25 AM5/22/15
to
Dear all,

I'm using an extern parameter and i get this error
"storage class specified for 'var1'"

//A.h

class A
{
public:
extern int var1;

......

}

// A.cc
#include "A.h"
void A:: function()
{
var1=x;
}


// B.h

#include "A.h"
class B
{
public int var1;

}

//B.cc

void B:: function2()
{
var1=Y;
}

So any help please ??

Best regards.
Ghada Glissa.

Juha Nieminen

unread,
May 22, 2015, 6:28:40 AM5/22/15
to
ghada glissa <ghada...@gmail.com> wrote:
> class A
> {
> public:
> extern int var1;

Member variables can't be "extern". It's semantically nonsensical.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

ghada glissa

unread,
May 22, 2015, 6:44:10 AM5/22/15
to
But i need to use this variable in other module, so what shall i do ?

Victor Bazarov

unread,
May 22, 2015, 7:20:57 AM5/22/15
to
On 5/22/2015 6:44 AM, ghada glissa wrote:
> But i need to use this variable in other module, so what shall i do ?

Use it. Its name is only available with an instance of that class, so
if you have an instance of that class, you should be able to us it, just
like you would any other member of any other class. Or did you have
some other meaning of "use" in mind?

V
--
I do not respond to top-posted replies, please don't ask

ghada glissa

unread,
May 22, 2015, 7:38:58 AM5/22/15
to
I'm working with a simulator, specially with modules and i want a variable shared between this two modules or classes so I opted for an extern variable,
but i get this error "storage class specified"

Victor Bazarov

unread,
May 22, 2015, 7:46:07 AM5/22/15
to
Have you tried reading the FAQ, especially 5.8? You can find it here:
http://www.dietmar-kuehl.de/mirror/c++-faq/

Juha Nieminen

unread,
May 22, 2015, 9:28:49 AM5/22/15
to
ghada glissa <ghada...@gmail.com> wrote:
> I'm working with a simulator, specially with modules and i want a variable shared between this two modules or classes so I opted for an extern variable,
> but i get this error "storage class specified"

A member variable of a class doesn't exist unless you instantiate that
class. You can make the instance of the class 'extern', not one of its
member variables.

An "extern" member variable makes no sense semantically. It's a
nonsensical concept.

If you really want one single class variable to exist independently
of the class itself, make it 'static'. (That makes it a class variable
rather than a member variable.)

Victor Bazarov

unread,
May 22, 2015, 9:39:06 AM5/22/15
to
On 5/22/2015 9:28 AM, Juha Nieminen wrote:
> [..]
> If you really want one single class variable to exist independently
> of the class itself, make it 'static'. (That makes it a class variable
> rather than a member variable.)

A nitpick: a static (or class) variable is still a *member* variable.
To get to it one still needs to use either an instance of the class or
the name of the class. Yes, an instance is not strictly needed to gain
access to a static data member. The member "variables" not qualified as
static are formally referred to as "non-static data members" to
emphasize the difference with static ones.

I wonder what book out there exists that uses the term "class variable"
as opposed to "member variable" to refer to *static data members* in
comparison to *non-static data members*...

ghada glissa

unread,
May 22, 2015, 10:19:09 AM5/22/15
to
I want a variable shared between many modules or classes.
I think "extern" did that, but i'm not sur about its semantic

So please help me.

Message has been deleted

Victor Bazarov

unread,
May 22, 2015, 10:58:43 AM5/22/15
to
Please post a brief example (in C++) of how you would want to "share" it
"between many classes" if you can. The codes doesn't need to be
compilable, just needs to convey your idea, your intent. Example:

class C1 {
public:
int foo() { return [[here I need that variable]]; }
};

class C2 {
public:
int bar() { return [[here I also need that variable]]; }
};

(I am not sure how exactly you want to "share" it and that's why I am
asking. Also, Juha recommended a static data member instead, have you
tried it?)

ghada glissa

unread,
May 22, 2015, 11:17:48 AM5/22/15
to
//A.h

class A
{
public:
extern int global_var;

......

}

// A.cc
#include "A.h"
void A:: function()
{
global_var=x;
// send the packet to B module

}


// B.h

#include "A.h"
class B
{
public int global_var;

}

//B.cc

void B:: function2()
{
if(global_var==X)
{
//get the packet
global_var=Y;

}
}

Victor Bazarov

unread,
May 22, 2015, 12:46:35 PM5/22/15
to
Great!

So, you want it to be global, but you've probably heard or read
somewhere that global data are A BAD IDEA(tm), and besides, in C++ every
bit of data needs to be a member of some class (not true), so instead of
declaring your 'global_var' as global (at the namespace level and extern
as you would in C, for instance), you are looking for a way to pack it
into a class, yet use it everywhere.

Well, don't. Declare it outside of any class, give it an 'extern'
specifier and live happily ever after.

Ian Collins

unread,
May 22, 2015, 3:46:08 PM5/22/15
to
While you are reading the FAQ, also read up on how to correctly post to
Usenet!

--
Ian Collins

Juha Nieminen

unread,
May 25, 2015, 5:32:51 AM5/25/15
to
Victor Bazarov <v.ba...@comcast.invalid> wrote:
> A nitpick: a static (or class) variable is still a *member* variable.
> To get to it one still needs to use either an instance of the class or
> the name of the class. Yes, an instance is not strictly needed to gain
> access to a static data member. The member "variables" not qualified as
> static are formally referred to as "non-static data members" to
> emphasize the difference with static ones.
>
> I wonder what book out there exists that uses the term "class variable"
> as opposed to "member variable" to refer to *static data members* in
> comparison to *non-static data members*...

"Class variable" is common terminology (and is exactly what 'static' does
when specified for a variable inside a class).

http://en.wikipedia.org/wiki/Class_variable
0 new messages