Structure initialization not allowed?

Can anyone help me out with this code? Why can't I initialize a value for the arrays in a structure?

1
2
3
4
5
6
7
8
9
10
11
12
struct car
{
      
      const static int price[4];
      price[0]=43143;
      price[1]=34858;
      price[2]=39591;
      price[3]=13451;
      
      
      
}vehicle


The code gives me some errors like:
13 G:\Project101\main.cpp ISO C++ forbids declaration of `price' with no type

14 G:\Project101\main.cpp invalid in-class initialization of static data member of non-integral type `int[1]'

14 G:\Project101\main.cpp making `price' static

15 G:\Project101\main.cpp ISO C++ forbids initialization of member `price'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

struct car
{
    static const int price[4] ; // declare it
};
 
struct car2
{
    // declate with an initializer (constexpr)
    static constexpr int price[4] { 43143, 34858, 39591, 13451 } ;
};

int main()
{
    for( int p : car::price ) std::cout << p << ' ' ; std::cout << '\n' ;
    
    for( int p : car2::price ) std::cout << p << ' ' ; std::cout << '\n' ;
}

const int car::price[4] = { 43143, 34858, 39591, 13451 } ; // define 

constexpr int car2::price[4] ;  // define (without initializer) 

http://coliru.stacked-crooked.com/a/fa5ff2b3d83ae639
For some reason, the constexpr keyword is not working in the IDE i work with. ( I use bloodshed c++)
I would personally just do it in the constructor, but I believe you can't use a static const through this method(don't quote me on that.)

1
2
3
4
5
6
7
8
9
10
11
12
struct car
{
      
    int price[4];
	car()
	{
		price[0]=43143;
		price[1]=34858;
		price[2]=39591;
		price[3]=13451;
	}
};
> For some reason, the constexpr keyword is not working in the IDE i work with.

constexpr requires C++11.


> ( I use bloodshed c++)

Download a current version of Dev-C++ from here: http://orwelldevcpp.blogspot.com
And follow the instruction on this page to enable C++11 support: http://www.cplusplus.com/doc/tutorial/introduction/devcpp/
Another problem just popped up. I know it might not be relevant to the topic but can anyone tell me how do I call a structure from another file? I'm still a newbie so please don't go all mad on me.
As far as I know just put the structure in a header file and make sure that you include the header file from wherever you want to be able to use that structure.
Topic archived. No new replies allowed.