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

STL problem

Hello!

I have some problem with STL.

I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper class for a
primitive int with some methods. I don't show the Integer class because it
will not add any information to my problem. Main is using some STL function

Now to my problem.
If I do this sequence in main first create two Handle object myh1 and myh2.
Then I use push_front to first push in myh1 and then myh2.
Then I use the STL function for_each in this way
for_each(myList1.begin(), myList1.end(), writeOut);
This function for_each will write out the entire list by calling the global
function writeOut for each handle object in the
list. So in this case will this writeOut write 2 and then 1 which is
correct.
This function writeOut looks like
void writeOut(handle_t& h)
{ cout << *h.body;}

But now to the strange thing if I use for_each with the functionoperator in
this way instead.
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator looks like. The last parameter is calling the function
operator for each Handle in the list.
void operator()(Handle& temp)
{ cout << *temp.body; }
This function operator will be called for each handle object in the list.

When this for_each is executing is first 2 written and then 1 and then the
program krasch with this error message

An unhandled exception of type 'System.NullReferenceException' occurred in
lab4_c++.exe
Additional information: Object reference not set to an instance of an
object.

I can't understand why the program krasch when I use a function operator.

Have you any suggestion ?

//Tony

#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;

typedef Handle<Integer> handle_t;

void skrivUt(handle_t& h)
{ cout << *h.body;}

main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );

myList1.push_front(myh1);
myList1.push_front(myh2);

for_each(myList1.begin(), myList1.end(), writeOut); //This works fine
for_each(myList1.begin(), myList1.end(), handle_t()); //This statement
will krasch the application
}

#include "integer.h"
#include <iostream>
using namespace std;

template<class T>
class Handle
{
public:
Handle() {}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

bool operator==(const Handle& temp)
{ return *body == *temp.body; }

Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}

void operator()(Handle& temp) //Functionoperator
{ cout << temp.body << endl; }
private:
T* body;
int* ref_count;
};
Jul 23 '05 #1
3 1622
"Tony Johansson" <jo*****************@telia.com> wrote in
news:cT*********************@newsc.telia.net:
Hello!

I have some problem with STL.

I have two classes called Handle which is a template class and Integer
which is not a template class. The Integer class is just a wrapper
class for a primitive int with some methods. I don't show the Integer
class because it will not add any information to my problem. Main is
using some STL function

Now to my problem.
If I do this sequence in main first create two Handle object myh1 and
myh2. Then I use push_front to first push in myh1 and then myh2.
Then I use the STL function for_each in this way
for_each(myList1.begin(), myList1.end(), writeOut);
This function for_each will write out the entire list by calling the
global function writeOut for each handle object in the
list. So in this case will this writeOut write 2 and then 1 which is
correct.
This function writeOut looks like
void writeOut(handle_t& h)
{ cout << *h.body;}

But now to the strange thing if I use for_each with the
functionoperator in this way instead.
for_each(myList1.begin(), myList1.end(), handle_t() );
The function operator looks like. The last parameter is calling the
function operator for each Handle in the list.
void operator()(Handle& temp)
{ cout << *temp.body; }
This function operator will be called for each handle object in the
list.

When this for_each is executing is first 2 written and then 1 and then
the program krasch with this error message

An unhandled exception of type 'System.NullReferenceException'
occurred in lab4_c++.exe
Additional information: Object reference not set to an instance of an
object.

I can't understand why the program krasch when I use a function
operator.

Have you any suggestion ?

//Tony

#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;

typedef Handle<Integer> handle_t;

void skrivUt(handle_t& h)
{ cout << *h.body;}
I'm assuming this is your "writeOut" function...

main()
That should be:

int main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );

myList1.push_front(myh1);
myList1.push_front(myh2);

for_each(myList1.begin(), myList1.end(), writeOut); //This works
fine for_each(myList1.begin(), myList1.end(), handle_t()); //This
statement
will krasch the application
}

#include "integer.h"
#include <iostream>
using namespace std;

template<class T>
class Handle
{
public:
Handle() {}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

bool operator==(const Handle& temp)
{ return *body == *temp.body; }

Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}

void operator()(Handle& temp) //Functionoperator
{ cout << temp.body << endl; }
Shouldn't this line be:

{ cout << temp->body << endl; }

?
private:
T* body;
int* ref_count;
};

Jul 23 '05 #2
Tony Johansson wrote:

[snip]

#include "handle.h"
#include <list>
#include <algorithm>
using namespace std;

typedef Handle<Integer> handle_t;

void skrivUt(handle_t& h)
{ cout << *h.body;}

main()
You mean:

int main()
{
list<handle_t> myList1;
handle_t myh1( new Integer(1) );
handle_t myh2( new Integer(2) );

myList1.push_front(myh1);
myList1.push_front(myh2);

for_each(myList1.begin(), myList1.end(), writeOut); //This works
fine
for_each(myList1.begin(), myList1.end(), handle_t()); //This statement
will krasch the application
}

#include "integer.h"
#include <iostream>
using namespace std;
Is this part of the file "handle.h"? If so: it is considerted a Bad
Idea(tm) to import std in header files.

template<class T>
class Handle
{
public:
Handle() {}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

bool operator==(const Handle& temp)
{ return *body == *temp.body; }

Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}

void operator()(Handle& temp) //Functionoperator
{ cout << temp.body << endl; }
Do you really want to write the pointer to cout, or do you mean the
pointee, i.e., should this be:

{ cout << *temp.body << endl; }
private:
T* body;
int* ref_count;
};


Your class has pointer members but lacks a destructor. It will leak memory.
Also, if you have a destructor that you did not post, it might very well be
the source of the segfault. The temporary that you are passing to for_each
can get copied and destructed.
Best

Kai-Uwe Bux
Jul 23 '05 #3
Just one more thing:

Kai-Uwe Bux wrote:
Tony Johansson wrote:

[snip]
template<class T>
class Handle
{
public:
Handle() {}

Handle(T* body_ptr) //Constructor
{
body = body_ptr;
ref_count = new int(1);
}

bool operator==(const Handle& temp)
{ return *body == *temp.body; }

Handle(const Handle& h) //Copy constructor
{
body = h.body;
ref_count = h.ref_count;
(*ref_count)++;
}
This copy constructor will not work on objects constructed by Handle<T>():
Note that Handle<T>() sets body and ref_count to 0 (or leaves them
uninitialized, which would be as bad). In the copy constructor, the pointer
ref_count is dereferenced. If it was not initialized to actually point to
an int, you have undefined behavior.

void operator()(Handle& temp) //Functionoperator
{ cout << temp.body << endl; }
private:
T* body;
int* ref_count;
};

Best

Kai-Uwe Bux

Jul 23 '05 #4

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

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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)...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.