I’ve recently been told that Windows Visual Studio is one of the best IDEs for C++ development, so I decided to get it, but it’s my first time using it and I’m already getting a weird error. The following code:
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Player {
public:
string name = "Player";
};
int main() {
cout << "Works";
return 0;
}
returns error C2864: ‘Player::name’ : only static const integral data members can be initialized within a class. What is wrong? This code compiled in Codeblocks IDE. Please explain to me what is wrong I don’t understand.
In C++03, you cannot initialize the data member at the point of declaration. You can do it in the constructor(s).
In C++11, your code is fine, so it could be that you were compiling with C++11 support in Codeblocks.