473,856 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor returns wrong size of stl::list<T>??

Hi folks,
still foolin with SafeList...

anyhow why does the following code return the wrong size for SL2
any help greatly appreciated

Thanks:
Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>

template<typena me T>
class SafeList : private list<T> {
public:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor"
<<endl;
}
// SafeList(const SafeList<T>&);

//destructor
~SafeList(){}
using list<T>::push_f ront;
using list<T>::size;
using list<T>::insert ;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};

#endif

//*************** *************** *************** ******

#include <iostream>
#include "safeList.h "

using namespace std;

int main()
{
SafeList<int> SL;
SL.push_front(1 0);
SL.push_front(2 0);
SL.push_front(3 0);
SL.push_front(4 0);
int x = 1;
int y = 10;
int* a;
int* b;
a = &x;
b = &y;
cout << "The size of the list is: " << SL.size() << endl;
SafeList<int> SL2(a,b);
cout << "The size of the list is: " << SL2.size() << endl;
cout << "this should be 10 not 1" <<endl;
}
Jul 23 '05 #1
4 1787
Barry Hynes wrote:
Hi folks,
still foolin with SafeList...

anyhow why does the following code return the wrong size for SL2
any help greatly appreciated

Thanks:
Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>

template<typena me T>
class SafeList : private list<T> {
public:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor"
<<endl;
}
// SafeList(const SafeList<T>&);

//destructor
~SafeList(){}
using list<T>::push_f ront;
using list<T>::size;
using list<T>::insert ;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};


It would be more useful if you told us a) what you get and b) what you
expect. You are calling the size function from list, so exactly what you
set _size to I would expect to make no difference at all, as
list<T>::size is not going to either see it or have any interest in it.

Chris
Jul 23 '05 #2

"Barry Hynes" <hy****@mar.d fo-mpo.gc.ca> wrote in message
news:B2******** ***********@urs a-nb00s0.nbnet.nb .ca...
Hi folks,
still foolin with SafeList...

anyhow why does the following code return the wrong size for SL2
any help greatly appreciated

Thanks:
Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>

template<typena me T>
class SafeList : private list<T> {
public:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor"
<<endl;
}
// SafeList(const SafeList<T>&);

//destructor
~SafeList(){}
using list<T>::push_f ront;
using list<T>::size;
using list<T>::insert ;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};

#endif

//*************** *************** *************** ******

#include <iostream>
#include "safeList.h "

using namespace std;

int main()
{
SafeList<int> SL;
SL.push_front(1 0);
SL.push_front(2 0);
SL.push_front(3 0);
SL.push_front(4 0);
int x = 1;
int y = 10;
int* a;
int* b;
a = &x;
b = &y;
cout << "The size of the list is: " << SL.size() << endl;
SafeList<int> SL2(a,b);
cout << "The size of the list is: " << SL2.size() << endl;
cout << "this should be 10 not 1" <<endl;
}


The provided code wasn't minimal. Your problem is insert(pos, n, elem);
inserts at iterator position pos n copies of elem. n in your case is 1. So
it inserts one copy of the element 10. Thus the size of the list is 1.

/ Eric
Jul 23 '05 #3

"Chris Jefferson" <ca*@cs.york.ac .uk> wrote in message
news:42******** ******@cs.york. ac.uk...
Barry Hynes wrote:
Hi folks,
still foolin with SafeList...

anyhow why does the following code return the wrong size for SL2
any help greatly appreciated

Thanks:
Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>

template<typena me T>
class SafeList : private list<T> {
public:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor" <<endl;
}
// SafeList(const SafeList<T>&);

//destructor
~SafeList(){}
using list<T>::push_f ront;
using list<T>::size;
using list<T>::insert ;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};
It would be more useful if you told us a) what you get and b) what you
expect. You are calling the size function from list, so exactly what you
set _size to I would expect to make no difference at all, as
list<T>::size is not going to either see it or have any interest in it.


i expect 10 but i get 1...it is included in main.

i should not have to include _size in SafeList...i would like to use size()
from list<T> but i
cannot get the correct value. ;(

thanks
Barry
Chris

Jul 23 '05 #4
thank you...

owe u a beer...

thanks man

Barry

"Eric Lilja" <er************ *@yahoo.com> wrote in message
news:cv******** **@news.island. liu.se...

"Barry Hynes" <hy****@mar.d fo-mpo.gc.ca> wrote in message
news:B2******** ***********@urs a-nb00s0.nbnet.nb .ca...
Hi folks,
still foolin with SafeList...

anyhow why does the following code return the wrong size for SL2
any help greatly appreciated

Thanks:
Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>

template<typena me T>
class SafeList : private list<T> {
public:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor" <<endl;
}
// SafeList(const SafeList<T>&);

//destructor
~SafeList(){}
using list<T>::push_f ront;
using list<T>::size;
using list<T>::insert ;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};

#endif

//*************** *************** *************** ******

#include <iostream>
#include "safeList.h "

using namespace std;

int main()
{
SafeList<int> SL;
SL.push_front(1 0);
SL.push_front(2 0);
SL.push_front(3 0);
SL.push_front(4 0);
int x = 1;
int y = 10;
int* a;
int* b;
a = &x;
b = &y;
cout << "The size of the list is: " << SL.size() << endl;
SafeList<int> SL2(a,b);
cout << "The size of the list is: " << SL2.size() << endl;
cout << "this should be 10 not 1" <<endl;
}


The provided code wasn't minimal. Your problem is insert(pos, n, elem);
inserts at iterator position pos n copies of elem. n in your case is 1. So
it inserts one copy of the element 10. Thus the size of the list is 1.

/ Eric

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5640
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
1
1286
by: Joseph Lanctot | last post by:
I'm trying to create a class of nodes, where each node would contain a list of other nodes. Ideally I would like it to work like this: class node { list<node> children; }; However MS Development Environment 2002 V7.0 / MS .NET Framework V1.0 will not handle this class (g++ does). The following works but I must explicitly dereference the pointers twice (and once implicity for the
4
2492
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic in the standard, maybe I searched not enough thoroughly?
5
1924
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count; pageNumber = 1; }
6
13928
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an instance for the copy constructor). For example: C++ vector<classvClass;
0
1760
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57571
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
45
18944
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
35
5912
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
0
11066
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10702
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10391
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9536
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7934
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5763
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4584
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.