473,756 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error: invalid use of nonstatic data member

Hi everybody,

I read Scotte Meyer's "Effective C++" book twice and I know that
he mentioned something specific about constructors and destructors that
was related to the
following error/warning: "error: invalid use of nonstatic data member "

However, he did NOT mention this error in the book explicitly.It
happens always in the constructor when you try to initialize some data
members in the constructor and try to accsess other data members. Of
course,one can always move the initialization away from the
constructor , but that is not the goal:

UP_SQLPrepQuery ::StatementInte rnals::Statemen tInternals(bool & status)
: nParams(0),
capacity(0),
tuple_num(0),
alter_session_s tatement(false) ,
select_statemen t(false)
{sprintf(stmt_i nternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

The error is in the line:

sprintf(stmt_in ternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

Here , I try to get some value through a function. Scott said something
about NOT doing that in constructors, but I am not sure and I could
NOT find anything. The problem is with the Function
embeddedConnect ion.GetConnecti onInternals(), which should return a
pointer to an object.

I am using gcc (GCC) 3.4.2 under: x86_64 GNU/Linux
Please, advise.

Cheers,
Dragomir Stanchev

Oct 23 '06 #1
7 15004
It would help if you would actually give the related declarations...

-miles

--
Is it true that nothing can be known? If so how do we know this? -Woody Allen
Oct 23 '06 #2
Disclaimer: I don't read 'gnu.gcc.help' newsgroup, so I removed it
from my reply. BTW, did you know there is 'gnu.g++.help'?

The|Godfather wrote:
I read Scotte Meyer's "Effective C++" book twice and I know that
he mentioned something specific about constructors and destructors
that was related to the
following error/warning: "error: invalid use of nonstatic data member
"

However, he did NOT mention this error in the book explicitly.It
happens always in the constructor when you try to initialize some data
members in the constructor and try to accsess other data members. Of
course,one can always move the initialization away from the
constructor , but that is not the goal:

UP_SQLPrepQuery ::StatementInte rnals::Statemen tInternals(bool & status)
>nParams(0),
capacity(0),
tuple_num(0),
alter_session_s tatement(false) ,
select_statemen t(false)
{sprintf(stmt_i nternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}
So, _assuming_ it's written correctly, I can divine that 'nParams',
'capacity', 'tuple_num', 'alter_session_ statement', 'select_stateme nt',
are all non-static members of 'StatementInter nals' class, which resides
in 'UP_SQLPrepQuer y' namespace.

Nothing else can be said about the class or the body of the c-tor.
>
The error is in the line:

sprintf(stmt_in ternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}
So? What's "stmt_internals "? What's "embeddedConnec tion"?
Here , I try to get some value through a function. Scott said
something about NOT doing that in constructors, but I am not sure and
I could NOT find anything. The problem is with the Function
embeddedConnect ion.GetConnecti onInternals(), which should return a
pointer to an object.
There is no function 'embeddedConnec tion.GetConnect ionInternals()' .
The syntax suggests that 'GetConnectionI nternals' is a _member_ of
the class, of which 'embeddedConnec tion' _object_ is an instance.
If 'embeddedConnec tion' *is* a type, you need '::' instead of '.'
here:

... embeddedConnect ion::GetConnect ionInternals() ...

but it's just a guess, given absence of any information about it.
I am using gcc (GCC) 3.4.2 under: x86_64 GNU/Linux
Please, advise.
You should probably use G++...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 23 '06 #3
The|Godfather wrote:
I read Scotte Meyer's "Effective C++" book twice and I know that
he mentioned something specific about constructors and destructors that
was related to the
following error/warning: "error: invalid use of nonstatic data member "

However, he did NOT mention this error in the book explicitly.
Huh? Scotte (or Scott, as most people call him) mentioned something
specific related to that error but he did not mention that error
explicitly, so how do you know he was referring to that error
specifically?
It
happens always in the constructor when you try to initialize some data
members in the constructor and try to accsess other data members.
You can't *initialize* static data members in the ctor
(http://www.parashift.com/c++-faq-lit...tml#faq-10.10), but you
can *use* them there. Regarding the "other data members," see this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.7
Of
course,one can always move the initialization away from the
constructor ,
Right, to enforce proper initialization, especially in the case that
the constructor needs to call a virtual function. This is generally
superior to forcing the user to call an Init() function, which can
easily be forgotten, leaving the object uninitialized. This example is
drawn from Sutter and Alexandrescu's _C++ Coding Standards_ (Item 49):

class B // Hierarchy root
{
protected:
B() { /*...*/ }

// Called right after construction
virtual void PostInitialize( ) { /*...*/ }

public:

// Interface for creating objects
template<class T>
static std::auto_ptr<T Create()
{
std::auto_ptr<T p( new T );
p->PostInitialize ();
return p;
}
};

// A derived class
class D : public B { /*...*/ };

// Creating an initialized D object
std::auto_ptr<D p = D::Create<D>();
but that is not the goal:

UP_SQLPrepQuery ::StatementInte rnals::Statemen tInternals(bool & status)
: nParams(0),
capacity(0),
tuple_num(0),
alter_session_s tatement(false) ,
select_statemen t(false)
{sprintf(stmt_i nternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

The error is in the line:

sprintf(stmt_in ternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

Here , I try to get some value through a function.
You have not followed the FAQ on how to post code that doesn't work
(http://www.parashift.com/c++-faq-lit....html#faq-5.8). Not
only is it helpful, in this case it is necessary because you don't
provide enough information to divine what you are trying to do. Please
post a *minimal* but *complete* program (i.e., one that we can cut and
paste directly to our editors unchanged) that demonstrates your
problem.

Cheers! --M

Oct 23 '06 #4
Ok,
I apologize for not posting working code , just did NOT have any time
yesterday.
Here is full report now:

gcc --version: gcc (GCC) 3.4.2
uname -rmo: 2.6.5-7.201-smp x86_64 GNU/Linux
compile command:
gcc -c -I/users/dstanche/problem test1.cpp
Error Message:
"test1.cpp: In constructor
`test1::Stateme ntInternals::St atementInternal s()':
test1.cpp:8: error: invalid use of nonstatic data member
'test1::stmt_in ternals' "

CODE :

example.h:
------------
class example {

public:
example() {;}
inline int giveIt(){return 2;}
};

--------
test1.h
-------
#include <example.h>

class test1 :example{
class StatementIntern als;
public:

test1();
private:
StatementIntern als * stmt_internals;
};

---------
test1I.h
---------
class test1::Statemen tInternals
{
public:
StatementIntern als();
unsigned long *length;
bool select_statemen t;
int stmt_counter;

};
--------
test1.cpp
-------
#include <test1.h>
#include <test1I.h>

test1::Statemen tInternals::Sta tementInternals ()
: length(0), select_statemen t(0)

{
stmt_counter=st mt_internals->giveIt(); // THE PROBLEM LINE IS THIS
ONE
}

test1::test1(): stmt_internals( new StatementIntern als){}
int main() {

return 0;
}
-------

As you can see the code does not do anything special. I have NO idea
why the problem occurs. Please advice.

Dragomir Stanchev

mlimber wrote:
The|Godfather wrote:
I read Scotte Meyer's "Effective C++" book twice and I know that
he mentioned something specific about constructors and destructors that
was related to the
following error/warning: "error: invalid use of nonstatic data member "

However, he did NOT mention this error in the book explicitly.

Huh? Scotte (or Scott, as most people call him) mentioned something
specific related to that error but he did not mention that error
explicitly, so how do you know he was referring to that error
specifically?
It
happens always in the constructor when you try to initialize some data
members in the constructor and try to accsess other data members.

You can't *initialize* static data members in the ctor
(http://www.parashift.com/c++-faq-lit...tml#faq-10.10), but you
can *use* them there. Regarding the "other data members," see this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.7
Of
course,one can always move the initialization away from the
constructor ,

Right, to enforce proper initialization, especially in the case that
the constructor needs to call a virtual function. This is generally
superior to forcing the user to call an Init() function, which can
easily be forgotten, leaving the object uninitialized. This example is
drawn from Sutter and Alexandrescu's _C++ Coding Standards_ (Item 49):

class B // Hierarchy root
{
protected:
B() { /*...*/ }

// Called right after construction
virtual void PostInitialize( ) { /*...*/ }

public:

// Interface for creating objects
template<class T>
static std::auto_ptr<T Create()
{
std::auto_ptr<T p( new T );
p->PostInitialize ();
return p;
}
};

// A derived class
class D : public B { /*...*/ };

// Creating an initialized D object
std::auto_ptr<D p = D::Create<D>();
but that is not the goal:

UP_SQLPrepQuery ::StatementInte rnals::Statemen tInternals(bool & status)
: nParams(0),
capacity(0),
tuple_num(0),
alter_session_s tatement(false) ,
select_statemen t(false)
{sprintf(stmt_i nternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

The error is in the line:

sprintf(stmt_in ternals->stmt_name,"%ll x",embeddedConn ection.GetConne ctionInternals( __LINE__,
__FILE__)->prep_cnt++);
}

Here , I try to get some value through a function.

You have not followed the FAQ on how to post code that doesn't work
(http://www.parashift.com/c++-faq-lit....html#faq-5.8). Not
only is it helpful, in this case it is necessary because you don't
provide enough information to divine what you are trying to do. Please
post a *minimal* but *complete* program (i.e., one that we can cut and
paste directly to our editors unchanged) that demonstrates your
problem.

Cheers! --M
Oct 24 '06 #5
The|Godfather wrote:
Ok,
I apologize for not posting working code , just did NOT have any time
yesterday.
Here is full report now:

gcc --version: gcc (GCC) 3.4.2
uname -rmo: 2.6.5-7.201-smp x86_64 GNU/Linux
compile command:
gcc -c -I/users/dstanche/problem test1.cpp
Error Message:
"test1.cpp: In constructor
`test1::Stateme ntInternals::St atementInternal s()':
test1.cpp:8: error: invalid use of nonstatic data member
'test1::stmt_in ternals' "
[...]
If I compile this sample in MS-VC++ 8 the error message is simply that
stmt_internals is an undeclared identifier, which makes a bit more sense
to me than g++'s message. The 'forward' declaration in the definition of
test1 merely *declares* StatementIntern als, it does not define it and so
it is not a nested class in the classical sense.

class test1 : example {
class StatementIntern als; // declares class name
public:
test1();
private:
StatementIntern als * stmt_internals; // defines pointer
};

The definition of StatementIntern als is at the same lexical level as
test1 and at that point the private attribute of test1 is unknown.
Besides, C and C++ do not really support access to implicitly referenced
outer variables from lexically nested functions. The question is not
just whether to allow it (using public/private modifiers) but how to
implement it when you do. A more complicated stack layout is needed to
get at the proper variable instance. This is a feature that you will
find in Algol-60, Pascal and the like but not C(++). Nobody seems to
care enough to request it, though. I don't.

Regards, Jan
Oct 24 '06 #6
Jan van Mastbergen wrote:
The|Godfather wrote:
>Ok,
I apologize for not posting working code , just did NOT have any time
yesterday.
Here is full report now:

gcc --version: gcc (GCC) 3.4.2
uname -rmo: 2.6.5-7.201-smp x86_64 GNU/Linux
compile command:
gcc -c -I/users/dstanche/problem test1.cpp
Error Message:
"test1.cpp: In constructor
`test1::Statem entInternals::S tatementInterna ls()':
test1.cpp:8: error: invalid use of nonstatic data member
'test1::stmt_i nternals' "
[...]
If I compile this sample in MS-VC++ 8 the error message is simply that
stmt_internals is an undeclared identifier, which makes a bit more
sense to me than g++'s message. The 'forward' declaration in the
definition of test1 merely *declares* StatementIntern als, it does not
define it and so it is not a nested class in the classical sense.

class test1 : example {
class StatementIntern als; // declares class name
public:
test1();
private:
StatementIntern als * stmt_internals; // defines pointer
};
I think it's all much simpler. You cannot use 'stmt_internals ' data
member in the constructor of StatementIntern als [nested] class simply
because 'stmt_internals ' is not a member of 'StatementInter nals' and
needs an instance of 'test1' to be used.

The confusion is common among Java programmers switching to C++. In
Java an instance of an outer class _automatically_ contains an instance
of a nested class. In C++ it does not.

struct Outer {
int data;
struct Inner { // type defined inside another type
Inner() { // c-tor
int i = data; // what's "data"?
}
};
};

'Inner' does not have a member named "data". 'Outer' does. 'Outer'
does not have a data member of type 'Inner'. Constructing an 'Outer'
object does not automatically construct an 'Inner' object, nor does
any instance of 'Inner' have any pre-defined "path" to 'data'.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '06 #7
"The|Godfat her" <dr************ ***@gmail.comwr ites:
class test1::Statemen tInternals
{
public:
StatementIntern als();
unsigned long *length;
bool select_statemen t;
int stmt_counter;
};
....
test1::Statemen tInternals::Sta tementInternals ()
: length(0), select_statemen t(0)
{
stmt_counter=st mt_internals->giveIt(); // THE PROBLEM LINE IS THIS
ONE
....
As you can see the code does not do anything special. I have NO idea
why the problem occurs. Please advice.
Because the StatementIntern als class has no data member called
"stmt_internals ", so there's no way the StatementIntern als constructor
can use such a member.

StatementIntern als has exactly the data members you declared above, no
others.

-Miles

--
Occam's razor split hairs so well, I bought the whole argument!
Oct 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2970
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100; void functionName(int parameterName) }; void ClassName::functionName(int parameterName=constVarName)
5
5716
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
7
12025
by: bazley | last post by:
I've been tearing my hair out over this: #ifndef MATRIX2_H #define MATRIX2_H #include <QVector> template<class T> class Matrix2 { public:
9
7725
by: Zero | last post by:
cc -c -o member.o member.c In file included from member.c:3: packet.h:55: two or more data types in declaration of `construct_request' packet.h:55: long, short, signed or unsigned invalid for `construct_request' make: *** Error 1 Here is what line 55 in packet.h is: unsigned char *construct_request(unsigned char *packet);
6
4410
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
3
13735
by: Erik H. | last post by:
I have an ASPX page in which I am trying to bind a datagrid to a dataset pulled from Microsoft Access DB using code inline method. For some reason, the compiler is having a problem with 'using'. Any help here would be much appreciated. Thanks! Getting the following error: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify...
0
23509
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub) 5: Procedure call or argument is not valid. 6: Overflow. 7: Out of memory.
12
2529
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an argument. I was hoping to create a map<int, const char*that would be used in the showError member function of the TExceptionHandler class where the key (int) would be the error code and const char* the message printed out to the console. My question...
10
2524
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to actually define z, you also need to add
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.