const int initializer for Vector within a class

Jul 24, 2015 at 3:42pm
Hi There!!

I am trying to initialize a Vector within a class like this

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
class Cars {
public:
  const static int MY_CONSTANT = 4;
  
private:
  vector<string> cars (MY_CONSTANT);
};


But the compiler gives me this error related with the constant static variable MY_CONSTANT:

 
‘MY_CONSTANT’ is not a type


What am I doing wrong?

Thank you in advance!!
Jul 24, 2015 at 5:09pm
An in-class member initialiser must be a brace-or-equal-initializer
1
2
3
4
private:
  // vector<string> cars (MY_CONSTANT);
  std::vector<std::string> cars { MY_CONSTANT };
};


Avoid using namespace std; at namespace scope, particularly in a header file.
Should the vector cars be a static member of Cars?
As far as possible, avoid using two names which differ only in case at namespace or class scope.
Jul 26, 2015 at 5:34pm
Hi JLBorges!!

Thank you very much for your answer!

I will follow your advice.

All the best
Topic archived. No new replies allowed.