473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

in-class static array initialization HOWTO?

Hello there!.
I would like to create a class with an array as attribute. This array
is the same for all the objects so I'd like to declare it as static,
but the problem is: where and how should I define the elements of the
array, in the constructor?.

I've tried the following code but It doesn't work at all:

class hamiltonian{

private:
matrix** operators; //OK. perfect
int alpha; //Perfect, of course;
static double coeffs[5]={1,1,1,0.5,0.5} // Here comes the crash

public:

/* destructor */
/* constructor */
}

Please, can anybody tell me how to declare/define a static array of
doubles within a class?.

Thanks everybody.

Sep 13 '05 #1
4 4648
Javi wrote:
Please, can anybody tell me how to declare/define a static array of
doubles within a class?.


class hamiltonian
{
private:
static double coeffs[5];
};
double hamiltonian::coeffs[5]={1,1,1,0.5,0.5};

Regards,
Peter Jansson
Sep 13 '05 #2

Javi wrote:
Hello there!.
I would like to create a class with an array as attribute. This array
is the same for all the objects so I'd like to declare it as static,
but the problem is: where and how should I define the elements of the
array, in the constructor?.

I've tried the following code but It doesn't work at all:

class hamiltonian{

private:
matrix** operators; //OK. perfect
int alpha; //Perfect, of course;
static double coeffs[5]={1,1,1,0.5,0.5} // Here comes the crash

public:

/* destructor */
/* constructor */
}

Please, can anybody tell me how to declare/define a static array of
doubles within a class?.

Thanks everybody.


// In the header file
class A {
static const double coeffs[5];
};

// in the cpp file:
const double A::coeffs[] = { 1, 2, 3, 4, 5 };

Cheers! --M

Sep 13 '05 #3
* Javi:
class hamiltonian{

private:
matrix** operators; //OK. perfect
int alpha; //Perfect, of course;
static double coeffs[5]={1,1,1,0.5,0.5} // Here comes the crash
static double const coeffs[5];

public:

/* destructor */
/* constructor */
}


Semicolon.

If the above is in a header file, then place the definition in an
implementation file:

double const hamiltonian::coeffs[5] = {1, 1, 1, 0.5, 0.5};

Anyway, you can avoid much work by using std::vector and friends instead of
raw arrays and pointers.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 13 '05 #4


Thanks a lot for your help. I'm in a hurry and have no time to look
for the answer in dozens of books.

Best regards to everybody.
Javi

Sep 13 '05 #5

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

Similar topics

3
by: Curious Expatriate | last post by:
Hi- I'm completely stumped. I'm trying to write some code that will parse a file and rewrite it with all URLs replaced by something else. For example: if the file looks like this: <b>click...
0
by: Ben Eisenberg | last post by:
I'm trying to run a php script setuid. I've tried POSIX_setuid but you have to be root to run this. The files are located on a public access unix system and have me as the owner and nobody as the...
2
by: Felix | last post by:
Hi, I've a problem: I want to have the result of my Mysql Query in a Table in my php file. Now I've this: <?
1
by: James | last post by:
What is the best way to update a record in a MYSQL DB using a FORM and PHP ? Where ID = $ID ! Any examples or URLS ? Thanks
1
by: Patrick Schlaepfer | last post by:
Why this code is not working on Solaris 2.8 host. Always getting: PHP Fatal error: swfaction() : getURL('http://www.php.net' ^ Line 1: Reason: 'syntax error' in /.../htdocs/ming2.php on...
1
by: phpkid | last post by:
Howdy I've been given conflicting answers about search engines picking up urls like: http://mysite.com/index.php?var1=1&var2=2&var3=3 Do search engines pick up these urls? I've been considering...
1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
3
by: Quinten Carlson | last post by:
Is there a way to conditionally define a function in php? I'm trying to run a php page 10 times using the include statement, but I get an error because my function is already defined. The docs...
10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
4
by: Matt Schroeder | last post by:
Does anyone know how to count how many rows are in a mysql table? This is what I have, but it doesn't work right: <? $db = mysql_connect("localhost", "username", "password");...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...

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.