By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,968 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,968 IT Pros & Developers. It's quick & easy.

Assigning from a vector of objects to a variable

P: 22
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
Share this Question
Share on Google+
1 Reply


P: 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.