Class Array Initialization

I am trying to initialize a class with an array, however I keep getting an error in my int main.

Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class testclass
{
    public:
        int testnum[10];
        testclass(int testnum[])
        {
            BuildArray(testnum);
        }
        
    private:
        void BuildArray(int num[])
        {
            for(int i=0;i<10;i++)
            {
                testnum[i]=num[i];
            }
        }
};


I've tried this:
testclass test={{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};

And this:
testclass test={0,0,0,0,0,0,0,0,0,0};

And finally this:
testclass test={{0,0,0,0,0,0,0,0,0,0}};

Can someone please tell me what I'm doing wrong?
Last edited on
What errors are you getting?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

class Test
{
	public:
		int array[10];
};

int main()
{
	Test t = { 0 }; //assigns 0 to first element and the rest are default initalized to 0
	Test t2 = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
	return 0;
}


http://ideone.com/8hvm4X
I:\CodeBlocks\Games\SDL2\Platformer Game\main.cpp|44|error: could not convert '{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}' from '<brace-enclosed initializer list>' to 'testclass'|
You could always just create the array and use that instead of brace-enclosed initalizer list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

class testclass
{
    public:
        int testnum[10];
        testclass(int testnum[])
        {
            BuildArray(testnum);
        }
        
        void output( void ) const
        {
        	for( int i = 0; i < 10; ++i )
        	{
        		std::cout << testnum[i] << ' ';
        	}
        	std::cout << std::endl;
        }
        
    private:
        void BuildArray(int num[])
        {
            for(int i=0;i<10;i++)
            {
                testnum[i]=num[i];
            }
        }
};

int main()
{
	int a[10] = {0,1,2,3,4,5,6,7,8,9};
	testclass test = a;
	testclass test2{ a };
	testclass test3( a );
	
	test.output();
	test2.output();
	test3.output();
	
	return 0;
}


your code but I added output function.
I would rather use a brace-enclosed initializer list, but thanks for the feedback.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <initializer_list>
#include <algorithm>
#include <cassert>

struct testclass
{
    int n[10] = {};

    testclass(std::initializer_list<int> l)
    {
        auto end = l.size() <= 10 ? l.end() : l.begin() + 10;
        std::copy(l.begin(), end, n);
    }
};

int main()
{
    testclass t{ 1, 2, 3, 4, 5, 6 };
    for (unsigned i = 0; i<10; ++i)
        std::cout << t.n[i] << '\n';
}


http://ideone.com/ToO2u0

Interestingly enough, this code causes VC++ to barf on an internal error. Seems to be the in-class member initialization.

Well of course it barfs
int n[10] = {};

should be
int n[10];

and I'm trying to keep testclass as a class..

How can I make so I dont have to do this:
1
2
int testvar[10]={0,1,2,3,4,5,6,7,8,9};
testclass test={testvar};


but can do something like this:
testclass test={{0,1,2,3,4,5,6,7,8,9}};
Last edited on
Well of course it barfs
int n[10] = {};

should be
int n[10];

In-class member initialization is allowed by the standard, so no.. it shouldn't barf. It isn't diffcult to work around, however.


and I'm trying to keep testclass as a class..

A struct is a class for all intents and purposes. The only difference is that of the default access levels.


How can I make so I dont have to do this:
1
2
int testvar[10]={0,1,2,3,4,5,6,7,8,9};
testclass test={testvar};


but can do something like this:
testclass test={{0,1,2,3,4,5,6,7,8,9}};

See the code in the post preceding your last one.
Thanks for the help I finally have this working.
Final Code:
1
2
3
4
5
6
7
8
9
10
11
class testclass
{
    public:
        int testnum[10];

        testclass(std::initializer_list<int> testnum)
        {
            auto i= testnum.size()<=10 ? testnum.end() : testnum.begin()+10;
            std::copy(testnum.begin(),i,this->testnum);
        }
};
Last edited on
Line 8 in your code has no effect. If the initializer_list happens to contain more values than this->testnum can hold, the call to std::copy will happily write to memory it has no business writing to.
Thanks for pointing out, fixed.
Topic archived. No new replies allowed.