Reputation: 24061
How can I create a static array in my header file? I looked at some examples on stackoverflow but can't get them to work.
Thanks!
#ifndef DRUMKITLIBRARY_H_
#define DRUMKITLIBRARY_H_
class DrumKitLibrary
{
public:
static const char* const list[] = {"zip", "zam", "bam"};
};
#endif /* DRUMKITLIBRARY_H_ */
Upvotes: 2
Views: 1788
Reputation: 124652
Your compiler error is occurring because that's not how you initialize static data (well, static const integral types can be initialized that way, but that's it). You only declare your static data in the class definition, you define it outside of the class. However, you still have a possible issue.
The problem with defining static data in your header file is that every file which includes that header gets its own copy of the array. You are better served by declaring it in the header and defining it in an implementation file.
// A.h
class A {
public:
static const char *f[];
};
// A.cpp
#include "A.h"
const char *A::f[] = { "one", "two" };
Upvotes: 3
Reputation: 16582
You don't.
You declare it in a header and define it in the source.
Upvotes: 0