Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does the error message "...is static;only point of intiialization

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I get the following error message and I am not sure what it means.

----
double tots::m is static; only point of initialization is its declaration
----

I have the following defined at the beginning of my program


////////////

#include "form2.h"
#include "tots.h"

tots::tots():m(form2::num)

//////////////

....For some reason it does not like my declaration above. m is a double static variable and I want it to be initialized to the value of form2 in num...Any idea what's going on? Thanks
 

You can not initialize a static variable defined as a class member in the constructor. Even if you could you would need to pass in form2 so the constructor knows where to get form2::num. There is a difference with C and C++ static variables as follows:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.

Also, static data members must be initialized at file scope, even if private. So to solve your problem you will need to add this code after your class definition just to make the compiler happy and get an error free compile: double tots::m;

Now change the Constructor to accept a form2 class and set tots::m to equal form2::num. Will look like this when all is said and done.

double tots::m; // initialize at scope to 0
tots::tots(form2) // pass form2 into constructor
{
m = form2.num; // initialize m
}


Enjoy,

Brother C


tots():m
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top