CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Variables inside classes (http://www.cfd-online.com/Forums/openfoam/78074-variables-inside-classes.html)

tonyuprm July 12, 2010 11:30

Variables inside classes
 
Hi all,

I'm creating a class but I cannot declare variables inside my class to then use them in my member functions. Here is the error I get.

const scalar pi2=3.14;

error: ISO C++ forbids initialization of member ‚pi2‚
error: making ‚pi2‚ static

olesen July 13, 2010 02:27

Quote:

Originally Posted by tonyuprm (Post 266924)
Hi all,

I'm creating a class but I cannot declare variables inside my class to then use them in my member functions. Here is the error I get.

const scalar pi2=3.14;

error: ISO C++ forbids initialization of member ‚pi2‚
error: making ‚pi2‚ static

Like the error message says, you'll need to make the data member static, which means it is the same for every instance.

Code:

static const scalar pi2 = 3.14;
Note that in-header initialization only works for simple types, for more complex types you need to initialize them in the .C file.

BTW: if you really just need PI, you could also consider using the constants provided with OpenFOAM:
http://foam.sourceforge.net/doc/Doxy...stants_8H.html

tonyuprm July 13, 2010 13:16

That worked just fine. Thanks!


All times are GMT -4. The time now is 13:55.