Friday, May 22, 2015

Digest for comp.lang.c++@googlegroups.com - 25 updates in 6 topics

ghada glissa <ghadaglissa@gmail.com>: May 22 03:16AM -0700

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 <nospam@thanks.invalid>: May 22 10:28AM

> {
> public:
> extern int var1;
 
Member variables can't be "extern". It's semantically nonsensical.
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
ghada glissa <ghadaglissa@gmail.com>: May 22 03:44AM -0700

But i need to use this variable in other module, so what shall i do ?
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 07:20AM -0400

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 <ghadaglissa@gmail.com>: May 22 04:38AM -0700

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"
 
So any help please ??
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 07:45AM -0400

On 5/22/2015 7:38 AM, ghada glissa 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"
 
> So any help please ??
 
Have you tried reading the FAQ, especially 5.8? You can find it here:
http://www.dietmar-kuehl.de/mirror/c++-faq/
 
V
--
I do not respond to top-posted replies, please don't ask
Juha Nieminen <nospam@thanks.invalid>: May 22 01:28PM

> 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.)
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 09:38AM -0400

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*...
 
V
--
I do not respond to top-posted replies, please don't ask
ghada glissa <ghadaglissa@gmail.com>: May 22 07:18AM -0700

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.
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 10:58AM -0400

On 5/22/2015 10:18 AM, ghada glissa wrote:
> 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.
 
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?)
 
V
--
I do not respond to top-posted replies, please don't ask
ghada glissa <ghadaglissa@gmail.com>: May 22 08:17AM -0700

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

}
}
"Öö Tiib" <ootiib@hot.ee>: May 21 11:20PM -0700

On Thursday, 21 May 2015 22:40:17 UTC+3, Richard wrote:
> >defines, because when I do, it _is_ part of the implementation.
 
> If you're implementing the standard library, then double underscore
> identifiers are reserved for you.
 
You should also use those underscores when you are implementing
something that standard libraries will be built upon (IOW platform).
It is so because implementation should not expose ordinary names
(that do not contain these underscores) that may conflict with
program code. Being mostly templates and header-only it is difficult to
not expose names of dependencies.
 
 
> It's really that simple, but so many people (including tool vendors
> happily generating code from templates that creates application code
> using reserved identifiers) do not know this or understand it.
 
You are correct of course and it is annoying. It is often because
people read source codes of standard library or platform and mimic
the idioms in their application code.
Juha Nieminen <nospam@thanks.invalid>: May 22 09:48AM

> (that do not contain these underscores) that may conflict with
> program code. Being mostly templates and header-only it is difficult to
> not expose names of dependencies.
 
If you use underscores in your own code, you run the risk of your
library not working with another compiler or with a newer version
of the same compiler.
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
scott@slp53.sl.home (Scott Lurndal): May 22 01:56PM

>>defines, because when I do, it _is_ part of the implementation.
 
>If you're implementing the standard library, then double underscore
>identifiers are reserved for you.
 
As I said, I was implementing a standard library (libc, as a matter
of fact).
 
The entire implementation is allowed to use __, not just C++
header files.
legalize+jeeves@mail.xmission.com (Richard): May 22 03:11PM

[Please do not mail me a copy of your followup]
 
slp53@pacbell.net spake the secret code
>of fact).
 
>The entire implementation is allowed to use __, not just C++
>header files.
 
I thought I was being explicit enough, but apparently not, so I'll say
it again:
 
I agree with you.
 
When you are implementing the standard library you can use identifiers
with __.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Christof Warlich <christof.warlich1@gmail.com>: May 22 05:39AM -0700

Hi,
 
I'm writing a framework where it should be possible for the users of that framework to either pass ordinary C functions or C++ functors to some other function. Basically, this works fine, i.e. the following sample code compiles and works as expected:
 
#include <iostream>
#include <typeinfo>
template<typename F> struct PassNothing {
PassNothing(F f) {
(*f)();
}
};
struct PrintFunctor {
void operator()() {
std::cout << "Functor" << std::endl;
}
} printFunctor;
void printFunction() {
std::cout << "Function" << std::endl;
}
 
template<typename F> struct PassThis {
PassThis(F f) {
(*f)(this);
}
};
struct PrintThisFunctor {
void operator()(PassThis<PrintThisFunctor *> *self) {
std::cout << "Functor called from " << typeid(self).name() << std::endl;
}
} printThisFunctor;
void printThisFunction(void *self) {
std::cout << "Function called from " << typeid(self).name() << std::endl;
}
 
int main() {
PassNothing<PrintFunctor *> passNothingWithFunctor(&printFunctor);
PassNothing<void (*)()> passNothingWithFunction(printFunction);
 
PassThis<PrintThisFunctor *> passThisWithFunctor(&printThisFunctor);
PassThis<void (*)(void *)> passThisWithFunction(printThisFunction);
return 0;
}
 
Executing it on my system gives:
 
$ ./tst
Functor
Function
Functor called from P8PassThisIP16PrintThisFunctorE
Function called from Pv
 
But the last line of the output shows my issue: As the code is currently written, the type information of the calling "PassThis" class gets lost due to the upcast to "void *" done in "printThisFunction". But while I could get the proper type being passed one line above for "printThisFunctor", I'm unable to do the same for "printThisFunction".
 
Any ideas how this could be achieved, if possible with plain C++ 2003?
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 09:21AM -0400

On 5/22/2015 8:39 AM, Christof Warlich wrote:
> I'm writing a framework where it should be possible for the users of
that framework to either pass ordinary C functions or C++ functors to
some other function. Basically, this works fine, i.e. the following
sample code compiles and works as expected:
> Functor called from P8PassThisIP16PrintThisFunctorE
> Function called from Pv
 
> But the last line of the output shows my issue: As the code is
currently written, the type information of the calling "PassThis" class
gets lost due to the upcast to "void *" done in "printThisFunction".
 
I am not sure I understand what you mean. What "type information" did
you expect not to get "lost"? IOW, what do you *want* to have?
 
> But
while I could get the proper type being passed one line above for
"printThisFunctor", I'm unable to do the same for "printThisFunction".
 
You're unable because it causes infinite recursion.
 
> Any ideas how this could be achieved, if possible with plain C++ 2003?
 
How could *what* be achieved? You've hit the known wall of
impossibility to declare a function that has as an argument a pointer to
the same function (in your case wrapped in a template, which is the same
thing). There is no solution for it.
 
What is it you're trying to accomplish? Why do you think you need to
pass the pointer to "self" into that function? Are you implementing
some kind of reflection?
 
V
--
I do not respond to top-posted replies, please don't ask
Christof Warlich <christof.warlich1@gmail.com>: May 22 07:27AM -0700

> I am not sure I understand what you mean. What "type information" did
> you expect not to get "lost"? IOW, what do you *want* to have?
 
I'd like to see the same behaviour for "printThisFunction" as I already got for "PrintThisFunctor": Passing the calling object's "this" pointer to operator()() without loosing its type information.
 
> > But while I could get the proper type being passed one line above for
> > "printThisFunctor", I'm unable to do the same for "printThisFunction".
 
> You're unable because it causes infinite recursion.
 
Yes, this is what is look like.
 
> impossibility to declare a function that has as an argument a pointer to
> the same function (in your case wrapped in a template, which is the same
> thing). There is no solution for it.
 
Ok, thanks for the clear statement: I just wanted to ensure that I did not
miss some obvious fix.
 
> What is it you're trying to accomplish? Why do you think you need to
> pass the pointer to "self" into that function? Are you implementing
> some kind of reflection?
 
No, it's some kind of "threading" framework, and passing "this" just allows to get hold of one's own "thread".
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 11:04AM -0400

On 5/22/2015 10:27 AM, Christof Warlich wrote:
> Victor Bazarov wrote:
(attribution was missing)
>> some kind of reflection?
 
> No, it's some kind of "threading" framework, and passing "this" just
> allows to get hold of one's own "thread".
 
I can't claim of deep understanding what a "threading framework" would
imply in such a case, forgive my ignorance. I always thought that
functions and functors ought to be written oblivious to threads in which
they are called. But you're the boss...
 
Perhaps if you showed how threads are figuring into your stuff, and for
what you needed the "this" in your code, folks who deal with threads
more would be able to advise you with higher precision.
 
V
--
I do not respond to top-posted replies, please don't ask
ram@zedat.fu-berlin.de (Stefan Ram): May 22 02:32PM

>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.
 
You seem to lack fundamentals of OOD and C++.
Teaching such fundamentals can be done better
by courses or books than via Usenet posts.
Juha Nieminen <nospam@thanks.invalid>: May 22 01:35PM

When declaring a class inside a namespace, especially if it's inside
multiple namespaces, there's a small but slightly annoying problem:
Indentation. In other words:
 
namespace NS1
{
namespace NS2
{
class MyClass
{
public:
// stuff
};
}
}
 
The contents of the class will be highly indented, which can be mildly
annoying. You could of course ignore the indentation of namespaces,
but that's bad form (and also annoying with editors that autoindent).
There is, however, a small trick to avoid that. Namely:
 
namespace NS1 { namespace NS2 { class MyClass; } }
 
class NS1::NS2::MyClass
{
public:
// stuff
};
 
I was wondering if the same trick could be used with nameless namespaces.
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
Victor Bazarov <v.bazarov@comcast.invalid>: May 22 10:13AM -0400

On 5/22/2015 9:35 AM, Juha Nieminen wrote:
> // stuff
> };
 
> I was wondering if the same trick could be used with nameless namespaces.
 
Only if you wrap the contents of the unnamed namespace in another, named
one:
 
namespace { class FooInAnonymous; }
class FooInAnonymous
{
public:
FooInAnonymous(int) {}
};
 
int main()
{
FooInAnonymous foo(42); // Error: 'FooInAnonymous' is ambiguous!
}
 
but
namespace { namespace _ { class FooInAnonymous; } }
 
class _::FooInAnonymous
{
public:
FooInAnonymous(int) {}
};
 
int main()
{
_::FooInAnonymous foo(42); // compiles OK, looks ugly.
}
 
V
--
I do not respond to top-posted replies, please don't ask
asetofsymbols@gmail.com: May 22 07:27AM -0700

namespace NS1
{namespace NS2
{class MyClass
{public:
// stuff

};
}
}
"Öö Tiib" <ootiib@hot.ee>: May 21 10:28PM -0700

On Friday, 22 May 2015 02:14:51 UTC+3, Christopher Pisz wrote:
> pow and <complex> pow. The actual error message is too long for me to be
> happy with copy pasting it.
 
> I think I can solve this problem just by using ::pow instead of std::pow.
 
C++11 standard reworded the 'pow' requirements and who knows what
Microsoft made out of it.
Perhaps it is confused because the size of string is unsigned type.
On that case casting it to double might be enough.
Paavo Helde <myfirstname@osa.pri.ee>: May 22 12:40AM -0500

Christopher Pisz <nospam@notanaddress.com> wrote in news:mjlljc$615$1@dont-
email.me:
 
> looked that up and it represents complex numbers. Fair enough. I don't
> need to deal with a complex number, but I don't know if I have to use it
> to get the solution to x^y.
 
You don't need to use complex numbers if you are not working with them. For
a start, try to ensure that both arguments passed to std::pow() are of the
same type.
 
[gibberish snapped]
 
hth
Paavo
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: