Assigning from a vector of objects to a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnSmith70
    New Member
    • Oct 2007
    • 22

    Assigning from a vector of objects to a variable

    I am storing previously entered values of a,b,c in a vector as you can see below. Later the user has the option to use one the objects from the vector, and assigns the value to the variables, which therefore are entered using the old values. But the problem is the object assigned are meaningless numbers(i.e. 32252355). How can I get the value of one of the old 'a'(eg. a of that particular slot) and assigned to the new 'a'?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    template <class A, typename B>
    class AClass
    {
    private:
      vector<A> aVector;
    public:  
      void AFunc(A t1)
      {     aVector.push_back(t1);
      }
       B get_aV(B i){
             return aVector[i].get_a();
      }
    };
     
    class BClass {
    private:
        int a,b,c;
    public:
        BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
    
    int get_a() {
    	return a;
    }
    };
    int main() {
       int a,b,c;
    //ordinarily a,b,c are entered from the keyboard(excluded)
    
       AClass<BClass,int> x;
    //later the user has the option of using the old values entered so
       AClass.AFunc(BClass (a,b,c));
       a = AClass.get_aV(1);
    //same for b and c respectively
       return 0;
    }
    If an object is made up of a,b,c as seen from the constructor, how can I get only the a of that object?
  • jabbah
    New Member
    • Nov 2007
    • 63

    #2
    aVector.push_ba ck() gets executed only once, so there is no
    aVector[1] but only an aVector[0].

    Comment

    Working...