Hello
In java I could keep static arrays like following.

class Test
{
public static int myArray[10];
}

Those arrays are static for any class.

In C++ how could we do this?

Recommended Answers

All 14 Replies

You can do exactly the same in C++:
in test.h

class Test
{
public:
 static int myArray[10];
}

in test.cpp

int Test::myArray[10] = {0}; // the = {0} is not neccessary, but good practice as it initialises the array with zeros

You declare the array in your class declaration but you have to define int once outside the class.

You can do exactly the same in C++:
in test.h

class Test
{
public:
 static int myArray[10];
}

in test.cpp

int Test::myArray[10] = {0}; // the = {0} is not neccessary, but good practice as it initialises the array with zeros

You declare the array in your class declaration but you have to define int once outside the class.

Thank you, that was the answer. BTW why can't i keep

int Test::myArray[10] = {0};

in the header file.

it's complains about multiple declarations (link time error).
If I need to keep this in the header what should i do?

in your header file, do following:

//at the very beginning add the following lines
#ifndef TEST_H_
#define TEST_H_

// your class and everything you want in this header file


#endif /* TEST_H_ */
//and this will be your last line

in your header file, do following:

//at the very beginning add the following lines
#ifndef TEST_H_
#define TEST_H_

// your class and everything you want in this header file


#endif /* TEST_H_ */
//and this will be your last line

Thanks, But still compaining.

#ifndef PREREQ_H
#define PREREQ_H

#define MAP_WIDTH 20
#define MAP_HEIGHT 20

class UtilFunc
{
	public:
		static int PositionMap[MAP_WIDTH*MAP_HEIGHT];
};

int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {0};

#endif

1>Linking...
1>TutorialFrameListener.obj : error LNK2005: "public: static int * UtilFunc::PositionMap" (?PositionMap@UtilFunc@@2PAHA) already defined in TutorialApplication.obj
1>AIModel.lib(MapSearchNode.obj) : error LNK2005: "public: static int * UtilFunc::PositionMap" (?PositionMap@UtilFunc@@2PAHA) already defined in TutorialApplication.obj
1>..\bin\Release\Test1.exe : fatal error LNK1169: one or more multiply defined symbols found

it shouldn't,

I just used it in a header file. Recompile the entire project.

//I tried it from my main and class in the header file and it works fine.
cout << UtilFunc::PositionMap[0];

You need to move the definition to a source file (.cpp), e.g.

// file: prereq.cpp
#include "prereq.h"

int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {0};

No, he doesnt need to,

here is the source code of my header file and it compiles and works fine:

//test.h
#ifndef TEST_H_
#define TEST_H_

#define MAP_WIDTH 20
#define MAP_HEIGHT 20

class UtilFunc
{
	public:
		static int PositionMap[MAP_WIDTH*MAP_HEIGHT];
};

int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {-1};

#endif /* TEST_H_ */

RE:
Permalink
13 Minutes Ago
This post is useful and well-written 0 This post is unclear
Re: Static array in C++
it shouldn't,

I just used it in a header file. Recompile the entire project.

C++ Syntax (Toggle Plain Text)

1.
//I tried it from my main and class in the header file and it works fine.
2.
cout << UtilFunc::PositionMap[0];

//I tried it from my main and class in the header file and it works fine. cout << UtilFunc::PositionMap[0];

from alwaysLearning0:

No it will complain because your variable is defined multiple times in multiple object files hence creating an ambiguity if you have got multiple files that include the header.

No, he doesnt need to

Yes he does -- it works for you because you include the header in one source file only.

Yes he does -- it works for you because you include the header in one source file only.

Thanks mitrmkar, hmm I guess that was the reason.
I have included it in multiple source files. Thought when it's static no multiple inclusion is made in link time.
seems it does.

@mitrmkar , You are right. !!!

Thanks.

int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {-1};

By the way: This will only initialise the first element of your array to -1 and all others to 0.

Also to keep in mind (as the question was originally asked by a Java programmer):
int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] id *NOT* a dynamic array but one that is fixed size at compiletime. If you want a dynamic array as the Java version mentioned above, then you have to either resort to allocating/deallocating with new/delete or the use of the STL containers std::vector, std::list, ... according to your requirements.

can some one tell me what was the problem and how it is solved,from the posts I understand that its solved, but how I am not sure

can some one tell me what was the problem and how it is solved,from the posts I understand that its solved, but how I am not sure

//header
#ifndef TEST_H_
#define TEST_H_

#define MAP_WIDTH 20
#define MAP_HEIGHT 20

class UtilFunc
{
	public:
		static int PositionMap[MAP_WIDTH*MAP_HEIGHT];
};

#endif

//cpp
int UtilFunc::PositionMap[MAP_WIDTH*MAP_HEIGHT] = {-1};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.