c++ - Loading different objects from file -


i'm trying load , b objects file like

a 3 4 b 2 4 5 b 3 5 6 2 3 

i have following classes, base, , b, subclasses of base. each operator>> overloaded.

the problem in function load(). i'm not sure how instanciate objects.. load() doesn't compile because ‘po’ not declared in scope. how fix ? or best way achieve ?

also, if manage make work somehow, need delete objects manually ?

class base {     public:         base() {}         base(int tot_) : tot(tot_) {}          void print() const {             std::cout << "tot : " << tot << std::endl;          }      private:         int tot; };  class : public base {     public:         a() : base(0) {}         a(int a1, int a2) : base(a1+a2) {} };   class b : public base {     public:         b() : base(1) {}         b(int b1, int b2, int b3) : base(b1+b2+b3){} };  std::istream& operator>>(std::istream& in, a& a) {      int a1, a2;     in >> a1;      in >> a2;      = a(a1,a2);      return in; }  std::istream& operator>>(std::istream& in, b& b) {      int b1, b2, b3;     in >> b1;      in >> b2;     in >> b3;      b = b(b1,b2,b3);      return in; }  bool load(const std::string& s, std::vector<base*>& objs) {     std::ifstream is(s.c_str());     if (is.good())     {         std::string obj;         while (!is.eof())          {             >> obj;             if (obj == "a") {                 *po = new a;             }             else (obj == "b") {                 b *po = new b;             }             >> *po;             objs.push_back(po);         }         is.close();         return true;     }     return false; } 

you have declare po in scope use it:

        >> obj;         base *po;         if (obj == "a") {             *a = new a;             >> *a;             po = a;         }         else { // (obj == "b")             b *b = new b;             >> *b;             po = b;         }         objs.push_back(po); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -