c++ - Error Variable is Protected -
#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void armyskirmish(); void battleoutcome(); string commander = ""; int numberofhumans = 0; int numberofzombies = 0; class armyvalues { protected: double attackpower; double defensepower; double healthpoints; public: void setattackpower(double a) { attackpower = a; } void setdefensepower(double d) { defensepower = d; } void sethealthpoints(double h) { healthpoints = h * (defensepower * .1); } }; class zombies: public armyvalues { }; class humans: public armyvalues { }; int main(int argc, char ** argv) { cout << "input commander's name: " << endl; cin >> commander; cout << "enter number of human warriors: " << endl; cin >> numberofhumans; cout << "enter number of zombie warriors: " << endl; cin >> numberofzombies; armyskirmish(); battleoutcome(); return 0; } void armyskirmish() { cout << "\nthe humans tense sound of undead shuffle towards them." << endl; cout << commander << " shuffles forward determined look." << endl; cout << "the undead form ranks , growl war chant!" << endl; cout << commander <<" shouts, charge!!!" << endl; cout << endl; cout << "warriors both sides blitz across field!" << endl; cout << endl; cout << "*the carnage has begun!*" << endl; cout << "*steal, sparks, , flesh flies" << endl; } void battleoutcome() { int zombielives = numberofzombies; int humanlives = numberofhumans; int randomnumber = 0; int humandeath = 0; int zombiedeath = 0; double newhumanlife = 0; double newzombielife = 0; zombies zombiebattledata; humans humanbattledata; srand(time(null)); zombiebattledata.setattackpower(20.0); humanbattledata.setattackpower(35.0); zombiebattledata.setdefensepower(15.0); humanbattledata.setdefensepower(20.0); zombiebattledata.sethealthpoints(150.0); humanbattledata.sethealthpoints(300.0); while(zombielives && humanlives > 0) { randomnumber = 1+(rand()%10); if(randomnumber < 6) { newhumanlife = humanbattledata.healthpoints - zombiebattledata.attackpower; if(newhumanlife <= 0) { humanlives--; humandeath++; } }else { newzombielife = zombiebattledata.healthpoints - humanbattledata.attackpower; if(newzombielife <= 0) { zombielives--; zombiedeath++; } } } if(zombielives <= 0) { cout << "humans have emerged victorious!" << endl; cout << "human deaths: " << humandeath << "zombie deaths: " << zombiedeath << endl; }else if(humanlives <= 0) { cout << "zombies have emerges victorious!" << endl; cout << "human deaths: " << humandeath << "zombie deaths: " << zombiedeath << endl; } i know code wont run of now. doing test run make sure receiving no errors. 2 errors i'm getting are:
armysimulatormain.cpp:25:10: error: 'double armyvalues::healthpoints' protected armysimulatormain.cpp:115:67: error: within context.newhumanlife = humanbattledata.healthpoints - zombiebattledata.attackpower;
this case attack power , health power however, defense power clearing errors. don't understand why getting flagged. i'm changing variable through public function shouldn't allowed?
also, i'm calling 3 variables outside of functions because being used multiple functions. how can plug variables somewhere don't floating freely above everything?
thanks guys can't believe forgot getters... anyway code runs appreciated i'll make sure remember time xd
it's not complaining line set values; say, uses public function. here, try read protected member variables:
newhumanlife = humanbattledata.healthpoints - zombiebattledata.attackpower; you try read 2 variables, , ones complains about.
you'll need public getter function read values.
Comments
Post a Comment