unknown error

this compiles on my other computer, but not this one.

What is wrong with this data member? Why would it compile on one pc, but not another (both OS's are the same and same version, using roughly the same compiler version )

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <vector>

class Utilities{
	public:
		std::string s = "";
};


int main(){
	Utilities util;
}




1
2
3
test2.cpp:7:19: sorry, unimplemented: non-static data member initializers
test2.cpp:7:19: error: in-class initialization of static data member ā€˜sā€™ of non-literal type
Last edited on
Being able to initialize non-static members in the class like that is a C++11 feature that not all compilers support. It's likely that the compiler on your other computer was a later version and supported it, whereas the compiler you're using now is older and does not.

Regardless... it's pointless to construct a string with "" because strings are default constructed to be empty anyway. So you can do this and literally have the exact same result:

1
2
3
4
class Utilities{
	public:
		std::string s;
};
well that was a way to re-occur the error at its minimum amount. the string had data in it, i just removed it.

i get an error even when not initializing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

class Utilities{
	public:
		std::string s;
		s = "test";
};


int main(){
	Utilities util;
}

but maybe thats just the c++11 thing. time to upgrade this pc's gcc then

thanks.

oh wow, the 4.8 g++ compiler has a carat indicator
1
2
3
test2.cpp:8:3: error: ā€˜sā€™ does not name a type
   s = "test";
   ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

class Utilities{
	public:
		std::string s;
		s = "test";
};


int main(){
	Utilities util;
}


i still dont know why i am getting this error though, i upgraded gcc and g++ from 4.6 to 4.7, then from 4.7 to 4.8, and i still get the same error on this computer. the other computer is using 4.7. What else could it be?
Last edited on
1
2
3
4
5
class Utilities{
	public:
		std::string s;
		s = "test";
};

That is illegal in any version of C++.


1
2
3
4
class Utilities{
	public:
		std::string s = "test";
};

is legal in C++11.
oh im stupid. I should not attempt to code when tired.
Topic archived. No new replies allowed.