Error: data member initializer is not allowed

When I try to initialize the char variable in my class it gives the error message in title, but if I create the variable in main it is fine? Can anyone see the problem?

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

class UserName{
public:
	UserName(){
		name;
		fullStop;
	}
	void setName(){
		cout << "Please enter your name! ";
		getline(cin,name);
	}
	string getName(){
		return name;
	}
private:
	string name;
	char fullStop = '.';
};

int main(){
	UserName UserName;
	UserName.setName();
	cout << UserName.getName();
}
Last edited on
closed account (j3Rz8vqX)
I'm not getting the error. GCC 4.7.1 windows 7 x86-64.

I am receiving warnings about line 8 and 9 though - they aren't doing anything; possibly remove if not necessary?

What exact error are you getting?
1>c:\users\alex\documents\visual studio 2012\projects\consoleapplication68\consoleapplication68\source.cpp(20): error C2864: 'UserName::fullStop' : only static const integral data members can be initialized within a class
Problem solved, I was hovering over the error on the variable fullStop and it give me the error message "Error: data member initializer is not allowed". I didn't compile it until just now but I think static is a type of data users give to the computer, I don't need to initialize that variable fullStop in the constructor do I? :D
Last edited on
gcc wrote:
non-static data member initializers only available with -std=c++11 or -std=gnu++11
one of the differences between the standards c++98 and c++11


> I don't need to initialize that variable fullStop do I?
¿uh? ¿why not?
Last edited on
closed account (j3Rz8vqX)
Your compiler doesn't like predefined variables in your class.

You can't initialize a member in the class definition unless it's static, const, and one of the integral types.

http://stackoverflow.com/questions/498433/resolving-only-static-const-integral-data-members-can-be-initialized-within-a-c

Try:
 
static const char fullStop = '.';// ? 


Integral types:
http://msdn.microsoft.com/en-us/library/exx3b86w%28v=vs.80%29.aspx

GCC seems to have allowed this exceptions in the newer compilers/c++11:

warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11


That's what I get if I compile without "-std=c++11".

Hope this helps.
> static const char fullStop = '.';
that has a different meaning.
Now is a class property (shared along all the objects of that class), and can't be changed.


PS: lines 8 and 9 are useless
My bad I meant "I don't need to initialize that variable fullStop in the constructor".
Thanks Dput that worked...
Here's my code, is there anything wrong with my layout? Regarding constructors/classes etc...?

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

class UserName{
public:
	UserName(){
		setName();
		welcomeName();
	}
	void setName(){
		cout << "Please enter your name! ";
		getline(cin,name);
	}
	void welcomeName(){
		cout << "Welcome " << name << fullStop << endl;
	}
	string getName(){
		return name;
	}
private:
	string name;
	static const char fullStop = '.';
};

int main(){
	UserName UserName;
}
Last edited on
Topic archived. No new replies allowed.