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? -
-
#include <iostream>
-
#include <string>
-
#include <list>
-
using namespace std;
-
-
int main() {
-
-
list<string> strList;
-
string strA = "a";
-
string strB = "b";
-
strList.push_back(strA);
-
strList.push_back(strB);
-
cout << "str list contains: " << endl;
-
list<string>::iterator sIter;
-
for (sIter=strList.begin(); sIter!=strList.end(); sIter++) {
-
string currStr = *sIter;
-
cout << " * " << currStr << endl;
-
}
-
-
list<test_obj*> theList;
-
test_obj* one_ptr = new test_obj("first","object");
-
theList.push_back(one_ptr);
-
test_obj* two_ptr = new test_obj("second","object");
-
theList.push_back(two_ptr);
-
-
cout << "test list contains: " << endl;
-
list<test_obj*>::iterator iter;
-
for (iter=theList.begin(); iter!=theList.end(); iter++) {
-
test_obj* currObj_ptr = *iter;
-
cout << " * " << (*currObj_ptr).getA() << ", " << (*currObj_ptr).getB() << endl;
-
}
-
-
return 0;
-
}
-
The test_obj code is very simple: -
#include "test_obj.h"
-
#include <string>
-
using namespace std;
-
-
string a;
-
string b;
-
-
-
test_obj::test_obj(string a_val, string b_val) {
-
-
a = a_val;
-
b = b_val;
-
}
-
-
string test_obj::getA() {
-
return a;
-
}
-
-
string test_obj::getB() {
-
return b;
-
}
-
-
Output of run is:
str list contains:
* a
* b
test list contains:
* second, object
* second, object
Thanks for your help,
Abe.
1 1107
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? -
-
#include <iostream>
-
#include <string>
-
#include <list>
-
using namespace std;
-
-
int main() {
-
-
list<string> strList;
-
string strA = "a";
-
string strB = "b";
-
strList.push_back(strA);
-
strList.push_back(strB);
-
cout << "str list contains: " << endl;
-
list<string>::iterator sIter;
-
for (sIter=strList.begin(); sIter!=strList.end(); sIter++) {
-
string currStr = *sIter;
-
cout << " * " << currStr << endl;
-
}
-
-
list<test_obj*> theList;
-
test_obj* one_ptr = new test_obj("first","object");
-
theList.push_back(one_ptr);
-
test_obj* two_ptr = new test_obj("second","object");
-
theList.push_back(two_ptr);
-
-
cout << "test list contains: " << endl;
-
list<test_obj*>::iterator iter;
-
for (iter=theList.begin(); iter!=theList.end(); iter++) {
-
test_obj* currObj_ptr = *iter;
-
cout << " * " << (*currObj_ptr).getA() << ", " << (*currObj_ptr).getB() << endl;
-
}
-
-
return 0;
-
}
-
The test_obj code is very simple: -
#include "test_obj.h"
-
#include <string>
-
using namespace std;
-
-
string a;
-
string b;
-
-
-
test_obj::test_obj(string a_val, string b_val) {
-
-
a = a_val;
-
b = b_val;
-
}
-
-
string test_obj::getA() {
-
return a;
-
}
-
-
string test_obj::getB() {
-
return b;
-
}
-
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
| |