473,386 Members | 1,815 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,386 software developers and data experts.

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

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
3 20786
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
3
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
10
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a...
3
by: Alexander Hans | last post by:
Hi, what's the usual way in C++ to define a static constant class member? Consider the following example: class TestClass { public: static const double a = 12.5; static const double b = 2...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.