C++ RPG Error: data member initializer is not allowed -


this has been posted several times, none of times have answered case. please me 'error: data member initializer not allowed' appears under equals signs. here's code problem in it.

//player.cpp :contains information player #include <iostream> #include <string> #include "main.cpp" using namespace std;  void player() { struct player { int charma = 0; unsigned int hunger = 10; unsigned int energy = 50; unsigned int health = 100; }; enum race { unknown, dead, human, orc, goblin, elf, lizard, cat, vampire, werewolf, snk }; } 

you getting error because initializing variables when declaring struct. not allowed. instead, move initialization constructor of struct.

however not error in code. defining struct inside of player function (which should constructor). need switch those, have player function inside of player struct. way struct have constructor can initialize values. thing, don't #include .cpp files. it's bad practice.

your code should this:

struct player { int charma; unsigned int hunger; unsigned int energy; unsigned int health;     player() : charma(0), hunger(10), energy(50), health(100)    {       // other constructor stuff here    } }; 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -