non-static data member initializers only available with -std

I wrote the following code to practice with..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <cstdlib>
#include <iostream>
using namespace std;

class Vehicle {
public:
     int price = 1000;
     int maxSpeed = 200;
};

class Toyota {
public:
    int price = 500;
    int maxSpeed = 3;
};

int main() {
    Toyota a;
    cout<<a.price;
}


However, when I run this code the following warning (during build) shows up:
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]


But the application can run fine.

Does anybody know what I'm doing wrong?

Last edited on
You are supposed to initialize in your constructor not in the class itself.

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
#include <cstdlib>
#include <iostream>
using namespace std;

class Vehicle 
{
public:
	Vehicle();
	int price;
	int maxSpeed;
};

Vehicle::Vehicle()
{
	int price = 0;
	int maxSpeed = 0;
}

class Toyota 
{
public:
	Toyota();
	int price;
	int maxSpeed;
};

Toyota::Toyota()
{
	price = 0;
	maxSpeed = 0;
}

int main() 
{
	Toyota a;
	cout<<a.price;
}


Side note: your code would not compile in my compiler (Microsoft visual studio 2012 express) which is what i would expect.
Last edited on
Also, consider that Toyota should not be in a class of it's own at this stage. Rather have a member variable Make in the vehicle class.

Hope all goes well.
The compiler simply informs you that your code is valid provided that it is compiled according to the new C++ Standard.
Thanks guys!
Topic archived. No new replies allowed.