0

I'm relearning C++ and I'm having some trouble with arrays in classes. Here's a simplified version of what I'm working with

class Class
{
private:
 string array[2];
public:
 Class()
 {
 array[2] = {"Hello", "World"};
 }
 void printOut(int x)
 {
 cout << array[x];
 }

Visual Studio has an error on the first brace in the array initialization in the constructor (i.e. {"Hello", "World"}; which says "Error: expected an expression." However, this problem does not occur when I initialize any other variable (not arrays).

I would have simple initialized the array values when I declared the array in the private section of the class as shown below.

class Class
{
private:
 string array[2] = {"Hello", "World"};

But Visual Studio gives an error on the equals sign saying "Error: data member initialization is now allowed." This error does occur any time I try to initialize the variables at the same time they are declared in the private section of the class.

Any help or advice would be appreciated, thank in advance.

asked Jan 1, 2013 at 20:00

1 Answer 1

3

Arrays can only be initialized using that syntax, not assigned. You have to initialize it in the constructor's initialization list:

Class() : array{"Hello", "World"} {}

Alternatively, use std::array which can be assigned.

std::array<string, 2> array;
 Class()
 {
 array = {{ "Hello", "World" }};
 }

Another:

Class()
{
 array[0] = "Hello";
 array[1] = "World";
}
answered Jan 1, 2013 at 20:02
Sign up to request clarification or add additional context in comments.

11 Comments

also, he is attempting to assign those two values to JUST the second element [2]. Array initialization requires there be no element specifier.
@Need4Sleep: He clearly wanted to assign the whole array, and was writing array[2] = because he's used to local variables written as string array[2] = {...}; Pubby's fix is correct.
The parentheses can be removed from the ctor-initializer-list, can't they? Just Class() : array{"Hello", "World"} {}
@BenVoigt Yes, and actually the parenthesis were a compile error. Fixed now.
@Pubby I tried both, neither worked. With the first method, I tried both with and without the parenthesis as Ben Voigt mentioned. I get 3 errors: 1) on the first brace, "Error: expected a "(""; 2) on the end brace "Error: expected a ";"" (adding a ";" did nothing); and 3) on the first brace of the constructor, "Error: expected a declaration." With the second method (std::array) I put the declaration in the private section and initialized it in the constructor; I still get "Error: expected an expression" in the same place.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.