By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

how do you declare and use a static constant array inside a class

P: n/a
Hi all,

I would like to have a static constant array inside a class definition
which would contain the number of days in each month (I am writing a
Date class as an exercise). However my attempts so far have been
unsuccessful.

Take this Test class as an example

// test.hpp
#include <ostream>
#include <string>
using namespace std;

#ifndef TEST_HPP
#define TEST_HPP

class Test
{
public:
static const int arr[]= {1,2,3}; // LINE 11
Test(string n);
friend ostream& operator<<(ostream& o, const Test& test);
private:
string name;
};

#endif

// test.cpp
#include "test.hpp"

Test::Test(string n)
{
name= n;
}

ostream& operator<<(ostream& o, const Test& test)
{
o << test.name << " " << Test::arr[0] << endl; // LINE 10
}

When I compile this I get the following errors (see the comments for
the line numbers);

test.hpp:11: error: a brace enclosed initializer is not allowed here
before the '{' token
test.hpp:11: error: invalid in-class initialization of static data
member of a non-integral type 'const int[]'
test.cpp:10: 'arr' is not a member of 'Test'

However if I were to declare a static const int variable, set it's
value in the header file and use it on line 10 in the same way I would
not get any problems;

replace LINE 11 in test.hpp with;

static const int a=0;

replace LINE 10 in test.cpp with;

o << test.name << " " << Test::a << endl;

So how should I declare a static constant array in the class and how
should I dereference it in functions outwith the class?

John

Mar 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* johnmmcparland:
Hi all,

I would like to have a static constant array inside a class definition
which would contain the number of days in each month (I am writing a
Date class as an exercise). However my attempts so far have been
unsuccessful.

Take this Test class as an example

// test.hpp
#include <ostream>
#include <string>
using namespace std;
It's not a good idea to put 'using namespace std;' in a header file.

#ifndef TEST_HPP
#define TEST_HPP
Include guard should be the very first (non-comment) in the file, just
as a matter of principle (e.g., avoid having the compiler needlessly
parse other includes).

class Test
{
public:
static const int arr[]= {1,2,3}; // LINE 11
You can declare it here, but you can't initialize it here.

Initialize it in a separate definition.
Test(string n);
friend ostream& operator<<(ostream& o, const Test& test);
private:
string name;
};

#endif
Since it seems you're placing all your code in headers (the hpp
extension implies a header file with all implementation code) the
easiest for you is probably to use a static constant in a member
function, like

class Test
{
private:
static char const* const* monthNames()
{
static char const* rawNames[] = { "jan", "feb", ... };
}
public:
...
};
--
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?
Mar 28 '07 #2

P: n/a
On 28 Mar, 14:04, "Alf P. Steinbach" <a...@start.nowrote:
*johnmmcparland:
Hi all,
I would like to have a static constant array inside a class definition
which would contain the number of days in each month (I am writing a
Date class as an exercise). However my attempts so far have been
unsuccessful.

Take this Test class as an example
// test.hpp
#include <ostream>
#include <string>
using namespace std;

It's not a good idea to put 'using namespace std;' in a header file.
#ifndef TEST_HPP
#define TEST_HPP

Include guard should be the very first (non-comment) in the file, just
as a matter of principle (e.g., avoid having the compiler needlessly
parse other includes).
class Test
{
public:
static const int arr[]= {1,2,3}; // LINE 11

You can declare it here, but you can't initialize it here.

Initialize it in a separate definition.
Test(string n);
friend ostream& operator<<(ostream& o, const Test& test);
private:
string name;
};
#endif

Since it seems you're placing all your code in headers (the hpp
extension implies a header file with all implementation code) the
easiest for you is probably to use a static constant in a member
function, like

class Test
{
private:
static char const* const* monthNames()
{
static char const* rawNames[] = { "jan", "feb", ... };
}
public:
...
};

--
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?
Ok I take on board what you said about hpp (I thought it was only a
way to distinguish between C++ headers and C headers) using namespace
std; and the #defines.

Regarding;
class Test
{
public:
static const int arr[]= {1,2,3}; // LINE 11
You can declare it here, but you can't initialize it here.
Initialize it in a separate definition.
I don't know what you mean. Declaring it (static const int arr[12];)
then initializing in the header doesn't work either. And I don't like
the idea of doing it in the function. It adds an unnecessary layer of
abstraction.

How should i declare and initialize this array?

Mar 29 '07 #3

P: n/a
On 29 Mar, 10:37, "johnmmcparland" <johnmmcparl...@googlemail.com>
wrote:
On 28 Mar, 14:04, "Alf P. Steinbach" <a...@start.nowrote:
*johnmmcparland:
class Test
{
public:
static const int arr[]= {1,2,3}; // LINE 11
You can declare it here, but you can't initialize it here.
Initialize it in a separate definition.

I don't know what you mean. Declaring it (static const int arr[12];)
then initializing in the header doesn't work either. And I don't like
the idea of doing it in the function. It adds an unnecessary layer of
abstraction.

How should i declare and initialize this array?
Header file:
class Test
{
public:
static const int arr[];
};

Implementation file:
const int Test::arr[3] = {1,2,3};

Gavin Deane

Mar 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.