473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Lists of Objects

5
Hello,

Another newbie question, regarding the code below.
While populating a list with strings allows me to see the strings I pushed into the list, when I do the same with instances of the class test_obj I get the same object twice on output. Obviously I got my pointers wrong, but how do I fix it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <list>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.   list<string> strList;
  10.   string strA = "a";
  11.   string strB = "b";
  12.   strList.push_back(strA);
  13.   strList.push_back(strB);
  14.   cout << "str list contains: " << endl;
  15.   list<string>::iterator sIter;
  16.   for (sIter=strList.begin(); sIter!=strList.end(); sIter++) {
  17.     string currStr = *sIter;
  18.     cout << " * " << currStr << endl;
  19.   }
  20.  
  21.   list<test_obj*> theList;
  22.   test_obj* one_ptr = new test_obj("first","object");
  23.   theList.push_back(one_ptr);
  24.   test_obj* two_ptr = new test_obj("second","object");
  25.   theList.push_back(two_ptr);
  26.  
  27.   cout << "test list contains: " << endl;
  28.   list<test_obj*>::iterator iter;
  29.   for (iter=theList.begin(); iter!=theList.end(); iter++) {
  30.     test_obj* currObj_ptr = *iter;
  31.     cout << " * " << (*currObj_ptr).getA() << ", " << (*currObj_ptr).getB() << endl;
  32.   }
  33.  
  34.   return 0;
  35. }
  36.  
The test_obj code is very simple:

Expand|Select|Wrap|Line Numbers
  1. #include "test_obj.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. string a;
  6. string b;
  7.  
  8.  
  9. test_obj::test_obj(string a_val, string b_val) {
  10.  
  11.   a = a_val;
  12.   b = b_val;
  13. }
  14.  
  15. string test_obj::getA() {
  16.   return a;
  17. }
  18.  
  19. string test_obj::getB() {
  20.   return b;
  21. }
  22.  
  23.  
Output of run is:

str list contains:
* a
* b
test list contains:
* second, object
* second, object


Thanks for your help,

Abe.
Aug 17 '07 #1
1 1107
ilikepython
844 Expert 512MB
Hello,

Another newbie question, regarding the code below.
While populating a list with strings allows me to see the strings I pushed into the list, when I do the same with instances of the class test_obj I get the same object twice on output. Obviously I got my pointers wrong, but how do I fix it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <list>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.   list<string> strList;
  10.   string strA = "a";
  11.   string strB = "b";
  12.   strList.push_back(strA);
  13.   strList.push_back(strB);
  14.   cout << "str list contains: " << endl;
  15.   list<string>::iterator sIter;
  16.   for (sIter=strList.begin(); sIter!=strList.end(); sIter++) {
  17.     string currStr = *sIter;
  18.     cout << " * " << currStr << endl;
  19.   }
  20.  
  21.   list<test_obj*> theList;
  22.   test_obj* one_ptr = new test_obj("first","object");
  23.   theList.push_back(one_ptr);
  24.   test_obj* two_ptr = new test_obj("second","object");
  25.   theList.push_back(two_ptr);
  26.  
  27.   cout << "test list contains: " << endl;
  28.   list<test_obj*>::iterator iter;
  29.   for (iter=theList.begin(); iter!=theList.end(); iter++) {
  30.     test_obj* currObj_ptr = *iter;
  31.     cout << " * " << (*currObj_ptr).getA() << ", " << (*currObj_ptr).getB() << endl;
  32.   }
  33.  
  34.   return 0;
  35. }
  36.  
The test_obj code is very simple:

Expand|Select|Wrap|Line Numbers
  1. #include "test_obj.h"
  2. #include <string>
  3. using namespace std;
  4.  
  5. string a;
  6. string b;
  7.  
  8.  
  9. test_obj::test_obj(string a_val, string b_val) {
  10.  
  11.   a = a_val;
  12.   b = b_val;
  13. }
  14.  
  15. string test_obj::getA() {
  16.   return a;
  17. }
  18.  
  19. string test_obj::getB() {
  20.   return b;
  21. }
  22.  
  23.  
Output of run is:

str list contains:
* a
* b
test list contains:
* second, object
* second, object


Thanks for your help,

Abe.
Well, running your code it works perfectly fine for me. I think your problem is in you test_obj.cpp file you have string a and b declared there. They should be in your class declaration. Once I deleted those two lines it worked fine.
Aug 17 '07 #2

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

Similar topics

9
by: Klaus Neuner | last post by:
Hello, I would like to understand the reason for the following difference between dealing with lists and dealing with strings: What is this difference good for? How it is accounted for in Python...
5
by: Wolfgang.Stoecher | last post by:
Hello, I'm new to Python and playing around. I'm confused by the following behaviour: >>> l1 = # define list >>> l2 = l1 # copy of l1 ? >>> l2,l1 (, ) >>> l2.extend(l1) # only l2 should...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
5
by: Jan Danielsson | last post by:
Hello all, I have written a simple whiteboard application. In my application, I want to be able to set draw attributes. This part works. I have a dictionary object which contains stuff like:...
2
by: James Stroud | last post by:
Hello All, I find myself in this situation from time to time: I want to compare two lists of arbitrary objects and (1) find those unique to the first list, (2) find those unique to the second...
2
by: Claire | last post by:
Is it safe to use linked lists of objects or is it likely that garbage collection will dump the nodes? thanks
11
by: Hakusa | last post by:
Pythonic lists are everything I want in a one dimensional array . . . but I'm trying to do a text adventure and simplify the proces by creating a grid as opposed to a tedious list of rooms this...
5
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly...
19
by: Dr Mephesto | last post by:
Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of...
1
by: colin | last post by:
Hi, I have 3 object of interest wich are objects in 3d space, surface,wire,point. they are all interconnected, and they all contain lists of objects they are connected to, eg each surface will...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.