Allocate array space in a class

I have a class and I need to add an array of strings to it. Something like this.

1
2
3
4
5
6
7
8
9
10
 class fcNav{

public:
  fcBar(int a);
   ...

private:
  char* color[7] = {"CT", "RD", "BL", "GN", "YW", "WT", "BK"};
   ...
};


I tried all kinds of things and all I could get past the compiler is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 class fcNav{

public:
  fcBar(int a);
   ...

private:
  char* color[7];
   ...
};

fcBar::fcBar(1){
  color[0] = "CT";
  ...
}


But I fear I am not allocating space for the array correctly.
Last edited on
Color is declared as having 7 elements of type char, a char holds 1 character, now count how many chars you're trying to fit in color. I'd use a vector of strings.
Last edited on
No, seven char pointers. One char pointer has space for one pointer.

Read about C++11 brace initialization, because there initialization of member arrays is described.
Color is defined as having 7 elements of type char

No, color is defined as having 7 elements of type char*, which can hold an array of characters, or a string of sorts.

@OP
What is the compiler saying? It looks like your constructor declaration and definition don't match:

In the class declaration:
fcBar(int a); //single-arg constructor

When the constructor is defined:
fcBar::fcBar() //no-arg constructor

Unless you didn't post the no-arg declaration, and the definition for the one with the int argument, I don't see anything else in the code you posted. Maybe post all of the code?
Last edited on
fcBar/fcBar.h:24: error: a brace-enclosed initializer is not allowed here before '{' token
fcBar/fcBar.h:24: error: ISO C++ forbids initialization of member 'color'
fcBar/fcBar.h:24: error: making 'color' static
fcBar/fcBar.h:24: error: invalid in-class initialization of static data member of non-integral type 'char* [7]'

is what I want

char color[7][2]. I tried to look up the brace initialization referenced above, but couldn't find anything specific enough.
Last edited on
This is a different approach, but have you tried enum color { CT, RD, BL, GN, YW, WT, BK };?
All im looking for is a list that I can reference so I can print the values on a small display. They are all 2 char strings.
how about a pair of chars?

[code]std::pair<char , char> colors(7);
Why don't you use a string array?

std::string color[7] = {"CT", "RD", "BL", "GN", "YW", "WT", "BK"};
If the strings are not intended to vary from class instance to class instance, make the array static.
thank you for your help. I have it working, now i need to play to understand better.
Topic archived. No new replies allowed.