472,811 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 1320
jabbah
63
aVector.push_back() gets executed only once, so there is no
aVector[1] but only an aVector[0].
Mar 6 '08 #2

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

Similar topics

0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.