468,484 Members | 2,059 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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'?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <class A, typename B>
  5. class AClass
  6. {
  7. private:
  8.   vector<A> aVector;
  9. public:  
  10.   void AFunc(A t1)
  11.   {     aVector.push_back(t1);
  12.   }
  13.    B get_aV(B i){
  14.          return aVector[i].get_a();
  15.   }
  16. };
  17.  
  18. class BClass {
  19. private:
  20.     int a,b,c;
  21. public:
  22.     BClass (int a1, int b1, int c1) : a(a1), b(b1), c(c1) {}
  23.  
  24. int get_a() {
  25.     return a;
  26. }
  27. };
  28. int main() {
  29.    int a,b,c;
  30. //ordinarily a,b,c are entered from the keyboard(excluded)
  31.  
  32.    AClass<BClass,int> x;
  33. //later the user has the option of using the old values entered so
  34.    AClass.AFunc(BClass (a,b,c));
  35.    a = AClass.get_aV(1);
  36. //same for b and c respectively
  37.    return 0;
  38. }
  39.  
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?
Mar 6 '08 #1
1 1161
jabbah
63
aVector.push_back() gets executed only once, so there is no
aVector[1] but only an aVector[0].
Mar 6 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.