hey all, i tried this a couple of different ways, but can't make it work. is it possible to overload a private struct anywhere in the same class?
hey all, i tried this a couple of different ways, but can't make it work. is it possible to overload a private struct anywhere in the same class?
i was playing around trying to find out if an overloaded could be placed in a class to access a structure without passing it back from main in an array. (that didn't make any sense did it?). i'm working with the limited amount of knowledge i have right now. i'm sure there's a more praticle way of doing something to this effect, but i haven't learned it yet. did i use the right vocabulary, please corret me if i'm wrong.
Code:#include <iostream.h> #define key 9 #define name_max 20 class c_stats { protected: struct plyr //data struct { char name[name_max]; float fldng; float b_aim; int spd; float thrwng; float b_ave; float p_ave; float lck; }; public: void mf_show (float x); c_stats(); }; void c_stats::mf_show(float x) { cout<<"a[0].b_aim: "<<x<<endl<<endl; } c_stats::c_stats() { ostream& operator<<(ostream& oput, plyr p[9]) //overload { int index; for(index=0;index<9;index++) { oput<<(p[index].name); oput<<(p[index].fldng); oput<<(p[index].b_aim); oput<<(p[index].spd); oput<<(p[index].thrwng); oput<<(p[index].b_ave); oput<<(p[index].p_ave); oput<<(p[index].lck); } return(oput); } plyr a[9]={ {"t", .9, .2, 9, .9, .333, .100, .5}, {"r", .6, .05, 4, 1, .220, .500, .5}, {"a",1,1,1,1,1,1,1}, {"b",1,1,1,1,1,1,1}, {"c",1,1,1,1,1,1,1}, {"d",1,1,1,1,1,1,1}, {"e",1,1,1,1,1,1,1}, {"f",1,1,1,1,1,1,1}, {"g",1,1,1,1,1,1,1}, }; main() { c_stats test; test.mf_show(); return(0); }
If you don't want to pass a plyr type struct into the operator each time you call it, make an instance of plyr struct in the class:
...and make an operator<< function a friend of c_stats class. This way you can define it explicitly and let it use private member m_player.Code:class c_stats { private: struct plyr { // ... } m_player; public: friend ostream &operator<<(ostream&, c_stats&); // ... };
Note: C doesn't allow nested functions, does it? You have the operator<< function in c_stats constructor?
Please excuse my poor english...
I have altered your code to something I think will work and be something like what you had intended.
Code:-------------------------------------------------------------------------------- #include <iostream.h> #define key 9 #define name_max 20 struct plyr //data struct { char name[name_max]; float fldng; float b_aim; int spd; float thrwng; float b_ave; float p_ave; float lck; }; class c_stats { protected: plyr a[key]; public: void mf_show (float x); c_stats(); friend ostream & operator<<(ostream& oput, c_stats p); }; void c_stats::mf_show(float x) { cout<<"a[0].b_aim: "<<x<<endl<<endl; } c_stats::c_stats() { a[9]={ {"t", .9, .2, 9, .9, .333, .100, .5}, {"r", .6, .05, 4, 1, .220, .500, .5}, {"a",1,1,1,1,1,1,1}, {"b",1,1,1,1,1,1,1}, {"c",1,1,1,1,1,1,1}, {"d",1,1,1,1,1,1,1}, {"e",1,1,1,1,1,1,1}, {"f",1,1,1,1,1,1,1}, {"g",1,1,1,1,1,1,1} } } ostream& operator<<(ostream& oput, c_stats p) //display all members of the array a present in the c_stats p { int index; for(index=0; index<9; index++) { oput<<(p.a[index].name); oput<<(p.a[index].fldng); oput<<(p.a[index].b_aim); oput<<(p.a[index].spd); oput<<(p.a[index].thrwng); oput<<(p.a[index].b_ave); oput<<(p.a[index].p_ave); oput<<(p.a[index].lck); cout << endl; } return(oput); } int main() { c_stats test; cout << test; test.mf_show(6.17); return 0; }
wow, thanks for all your help i didn't know it could be that easy. i pluged in everything i was trying to get, but it's returning numbers that don't match, like the array is off or something. i changed the x= in the for loop and still got the exact same numbers. when i set the array sans loop, it repeats a single number (like you'd expect), but it's not the right bytes. any ideas?
thanks again
Code:#include <iostream.h> #define key 9 #define name_max 20 struct plyr //data struct { char name[name_max]; float fldng; float b_aim; int spd; float thrwng; float b_ave; float p_ave; float lck; }; class c_stats { protected: plyr a[key]; public: void mf_show (); c_stats(); friend ostream & operator<<(ostream& oput, c_stats p); }; void c_stats::mf_show() { int x; for(x=0;x<key;x++) { cout<<a[x].b_aim<<endl<<endl; } }; c_stats::c_stats() { plyr a[key]={ {"t", .9, .2, 9, .9, .333, .100, .5}, {"r", .6, .05, 4, 1, .220, .500, .5}, {"a",1,1,1,1,1,1,1}, {"b",1,1,1,1,1,1,1}, {"c",1,1,1,1,1,1,1}, {"d",1,1,1,1,1,1,1}, {"e",1,1,1,1,1,1,1}, {"f",1,1,1,1,1,1,1}, {"g",1,1,1,1,1,1,1}}; }; ostream& operator<<(ostream& oput, c_stats p) //display all members of the array a present in the c_stats p { int index; for(index=0; index<9; index++) { oput<<(p.a[index].name); oput<<(p.a[index].fldng); oput<<(p.a[index].b_aim); oput<<(p.a[index].spd); oput<<(p.a[index].thrwng); oput<<(p.a[index].b_ave); oput<<(p.a[index].p_ave); oput<<(p.a[index].lck); cout << endl; } return(oput); } main() { c_stats test; test.mf_show(); return (0); }
hey, i'm still having problems with this. does someone mind taking a look?
Your constructor is initialising a local array, not your protected plyr a[key]. Also you don't need semi-colons after your method definitions.
how would I intialize the protected array? i guess it may sound stupid, but actually what do i write in the constructor?
take a careful look at the constructor in unregistered's code versus yours. Then review the messages after to review a likely solution. Note the absence of the plyr term in unregistered whereas it is present in yours. The plyr term in yours means that the a[key] in the constructor is local to the constructor and is not the same a[key] declared as a protecterd variable in the class per se, even thought the two entities have the same name, the same description, etc. If you leave out the plyr term in the constructor then the a[key] is the protected member variable in the class and not a local plyr array.