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;
}; 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; };
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
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...
|
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();...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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: 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: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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: 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...
| |