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

J

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;

#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
 
A

Alf P. Steinbach

* 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:
...
};
 
J

johnmmcparland

*johnmmcparland:
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;
};

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?
 
G

Gavin Deane

*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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top