473,408 Members | 1,769 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,408 software developers and data experts.

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<typename 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_front;
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(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
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 1771
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<typename 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_front;
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.dfo-mpo.gc.ca> wrote in message
news:B2*******************@ursa-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<typename 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_front;
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(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
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<typename 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_front;
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.dfo-mpo.gc.ca> wrote in message
news:B2*******************@ursa-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<typename 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_front;
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(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
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
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...
1
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...
4
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<>...
5
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;...
6
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...
0
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...
7
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...
45
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...
35
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.