So this is my program, what I want to do is create a character from a game with certain stats, strength, armor, critical chance, etc.
When I executed the program instantly crashes and says:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I've read it already, but it didn't work, could you separate this program in different files to see how to do it? If you don't have time tell me and I'll create a new post.
I have NEVER understood how this pointer works... I mean when do I use it? what does it do exactly?
Maybe now that you have brought this pointer out you could help me :D
Lines 12-23 go in header file (player.h).
Lines 25-67 go in a .cpp file (player.cpp)
Lines 69-94 go in main.cpp (or whatever you want to call your program)
You'll need to repeat lines 1-8 in both your .cpp files and add an #include of your header file.
You also need to get rid of the global at line 10. It belongs as a local inside main().
Line 47: You don't want your call to srand() inside set_random_stats(). That will reinitialize the RNG each time you call set_random_stats(). Move it to line 70.
Line 65-66: You don't need the new_player:: scope qualifiers here, although they're harmless.
@andy1192 - Yes new_player is indeed known. There's just no need for it here. Using the this-> pointer is not needed either since create_character() is a member of the new_player class.
@erik341 - The this pointer simply points to the current instance of the object. Not usually needed, but can be useful if you can be helpful if you have naming conflicts between arguments and members.
Consider if set_name() took an argument called name_player:
1 2 3 4 5 6
void new_player::set_name (string name_player)
{ name_player = name_player; // The compiler is going to be confused here because it does know if you're referring to the argument or the member variable.
// Instead:
this->name_player = name_player; // ambiguity resolved
this pointer can also be useful if you want to pass a pointer to the current object to a function that's not a member of the class.
Error C2864: 'new_player::name_player': only static const integral data members can be initiated within a class.
and IntelliSence reports:
data member initialization is not allowed
This seems to agree with your error message:
basic_string::_S_construct null not valid
Style issue:
If you have usingnamespace std;
in line 8, you don' t need the std:: prefix in lines 21 and 22. However, if you leave the namespace out of line 8, you'll need to add std:: before string, because string is in the std namespace. In other words, this: