c++ - How do I print out objects in a vector by overloading the << operator? -
ok i'm confused operator overloading stuff, syntax weird me , i'm not great @ programming anyway. looking around on net apparently think way me print out objects using cout << overload it. have vector of objects , if had regular vector of ints or strings i'd utilize iterator , go through each 1 , dereference print out whats in it, don't think technique working objects :-/ here have far...help!
barone.h //my header file #include <string> #include <vector> using namespace std; class barone { private: string name; string type; string size; vector<barone> bar; //vector of barone objects vector<barone>::iterator it; //iterator bar public: barone(); //constructor void addbottle(string, string, string); //adds new bottle bar void revealspace(); void printinventory(); friend ostream& operator<<(ostream& os, const barone& b); }; and implementation looks like:
barone.cpp //implementation #include "barone.h" #include <iostream> #include <string> using namespace std; barone::barone() { //adding 4 default bottles } void barone::addbottle(string bottlename, string bottletype, string bottlesize) { name = bottlename; type = bottletype; size = bottlesize; } void barone::printinventory() { (it = bar.begin(); != bar.end(); ++it) { cout << *it << endl; } } ostream& operator<<(ostream& os, const barone& b) { os << b.name << "\t\t\t" << b.type << "\t\t\t" << b.size; homecoming os; } so how come when phone call printinventory in main doesn't anything? did overloading wrong? syntax mistakes?
ok main too:
#include "barone.h" #include <iostream> #include <string> using namespace std; int main() { string tiqo, peruvian, wellington, smooze; string vodka; string rum; string whiskey; string small; string medium; string large; //default bottles vector<barone> bar; //vector of barone objects vector<barone>::iterator it; //iterator bar barone inventory; //barone object inventory.addbottle(tiqo, vodka, large); bar.push_back(inventory); inventory.addbottle(peruvian, rum, medium); bar.push_back(inventory); inventory.addbottle(wellington, vodka, large); bar.push_back(inventory); inventory.addbottle(smooze, whiskey, small); bar.push_back(inventory); ^^^thats piece of it...the rest formatting how things displayed when programme runs , stuff. ok i'll seek , separate classes suggested tho. addbottle adds info object in vector right? gets info , adds variables name, type , size , set vector "bar". or no?
you don't show main() program. code, class design confuses bar contents bar causing behavior see.
the operator << ok outputting info of bottle. i'm sure barone on called has empty bar vector. addbottle() doesn't add together anywhere (in particular not contained bar). instead sets properties (data members) of outer barone object.
the origin of confusion class design, barone apparently intended serve both bottle , bar (which contains bottles).
i suggest restart , seek separate barand bottleclasses.
btw: keeping iterator utilize in local loops class fellow member not idea. sooner or later run reentrancy problems such approach. loop iterators should local variables, preferably scoped loop.
c++ class visual-c++ vector operator-overloading
No comments:
Post a Comment