ISO C++ forbids initialization of member

I can't find the error and the system always return the errors "ISO C++ forbids initialization of member `username' ", "making `username' static ", "invalid in-class initialization of static data member of non-integral type `std::string' " and the same with psword. Can anybody help me?

Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include "login.h" 

using namespace std;

int main(){
    
    string us_name;
    string pd;
    
    cout << "Username: ";
    cin >> us_name;
    cout << "Password: ";
    cin >> pd;
    cout << endl << endl;
    
    system("PAUSE");
    return 0;
}

Login.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "login.h" 
#include <iostream>
#include <string>

using namespace std;

login::login(){
               username = "";
               psword = "";
	
}

login.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef LOGIN_H
#define LOGIN_H

#include <string>

using namespace std;

class login{
            
    public:
           
		login();
        void getUsername();
        void getPassword();    
           
	private:
            
            string username = "Username";
            string psword = "Password";	
		
            
};

#endif 
You can't initialize the variables username, psword in the login class. Just initialize them in the constructor.
You shouldn't be assigning values in a header file, unless as the message says, the variable is static. You can use an initialiser list in a constructor though, in the .cpp file:

1
2
3
4
login::login() :  // colon introduces initialiser list
                 username("Username"),
                 psword("Password")
{}


The implementation file (.cpp say) is the place to do ordinary assignment.

Hope all goes well.
you don't initialize variables inside a class so string username = "Username"; should be string username; and string psword = "Password"; should be string psword;
@joaoareias: Your code should compile in C++11. Maybe your compiler is too old or you have to turn on C++11 features somehow.
Thanks guys
Topic archived. No new replies allowed.