#include <iostream>
#include <cstring>
#include <QString>
using namespace std;
class A {
public:
static const int i = 9;
static const int PI = 1.3;
static const char ch = 's';
static const string str = "hello world"; // <--- error
static const QString str2 = "hello world"; // <--- error
};
int main(int argc, char **argv) {
cout << "Hello world" << endl;
return 0 ;
}
As the code gives everything, how can I init a string.
Non-integral type members (that includes
stringand your user-defined types), need to be initialized outside the class definition, in a single implementation file (.ccor.cppusually).In your case, since you didn’t separate the class definition in a header, you can initialize the
statics right after your class:EDIT: Besides this, as Nawaz pointed out, the header file that defines
stringis<string>, not<cstring>.