Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:34:10+00:00 2026-06-04T06:34:10+00:00

After reading How to initialize an array in C , in particular: Don’t overlook

  • 0

After reading How to initialize an array in C, in particular:

Don’t overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

I tried something like this:

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something() {
    myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

int ShowThingy(int what) {
    return myArray[what];
}

~Something() {}
};

int main () {
   Something Thing;
    std::cerr << Thing.ShowThingy(3);
}

And I get:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment

The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.

I tired:

private:
    int * myArray;

public:
    Something() {
            myArray = new int [10];
            myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}

This looked funky to me to, and so to the compiler:

..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment

This also did not work:

private:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

with:

 ..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token
 ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers
 ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type

I have been doing really good and learning what does not work, but not so good learning what does work.

So, how do I used initialization lists {value, value, value} for an array inside a class?

I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:34:11+00:00Added an answer on June 4, 2026 at 6:34 am

    You need to initialize the array in the constructor initialization list

    #include <iostream>
    
    class Something {
    private:
    
    int myArray[10];
    
    public:
    
    Something()
    : myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
    {
    }
    
    int ShowThingy(int what) {
        return myArray[what];
    }
    
    ~Something() {}
    };
    
    int main () {
       Something Thing;
        std::cerr << Thing.ShowThingy(3);
    }
    

    ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

    C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int startingPoint[2]={i,j}; I got this after reading an online code.Is this correct? Means if
After reading a bit on Javascript's prototypical inheritance model , I change my style
After reading Dynamically calling unmanaged dlls in .net I've been trying to modify the
After reading this post I kinda felt in the same position as the guy
After reading this article on thedailywtf.com, I'm not sure that I really got the
After reading the wikipedia article on WPF architecture, I am a bit confused with
After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated , I was
After reading http://www.w3schools.com/tags/tag_noscript.asp confused about onclick event on noscript tag. Anybody knows the purpose?
After reading the mkdir(2) man page for the Unix system call with that name,
After reading a description about swapping pointer addresses on Stackoverflow, I have a question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.