| View previous topic :: View next topic |
| Author |
Chapter 11 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 462 Location: Amsterdam, the Netherlands
|
Posted: Wed Dec 30, 2009 9:39 pm Post subject: |
|
|
OK code?
#include <list>
#include <iostream>
using namespace std;
///
class Base
{ // Base class
private:
public:
Base() { }
public:
virtual ~Base() { cout << "Base destructor\n\n"; }
};
class Derived : public Base
{ // Derived class
private:
public:
Derived() : Base() { }
~Derived() { cout << "Derived destructor\n"; }
};
int main()
{
{ // Normal situation
Base* d1 = new Derived;
Base* d2 = new Derived;
list<Base*> myList; myList.push_back(d1); myList.push_back(d2);
list<Base*>::iterator it;
for (it = myList.begin(); it != myList.end(); ++it)
{
delete *it;
}
cout << "size of list " << myList.size() << endl;
}
return 0;
}
|
|
| Back to top |
|
 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 462 Location: Amsterdam, the Netherlands
|
Posted: Sun Mar 01, 2009 2:31 pm Post subject: |
|
|
Thanks,
You are 100% correct. The vectors mut be sorted (see Josuttis STL book page 416)
// I added the following two lines:
sort(myVec3.begin(), myVec3.end());
sort(myVec4.begin(), myVec4.end());
|
|
| Back to top |
|
 |
pierrechristophe
Joined: 20 Feb 2009 Posts: 9 Location: Singapore
|
Posted: Sun Mar 01, 2009 10:52 am Post subject: Original and modified file |
|
|
Hello,
Please find both files
| Description: |
|
 Download |
| Filename: |
HelloWorldAlmost.cpp |
| Filesize: |
3.9 KB |
| Downloaded: |
112 Time(s) |
| Description: |
|
 Download |
| Filename: |
HelloWorldAlmost_Modified.cpp |
| Filesize: |
4.25 KB |
| Downloaded: |
107 Time(s) |
|
|
| Back to top |
|
 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 462 Location: Amsterdam, the Netherlands
|
Posted: Fri Feb 27, 2009 2:51 pm Post subject: |
|
|
| Please post the full code and we can compile it. Thanks.
|
|
| Back to top |
|
 |
pierrechristophe
Joined: 20 Feb 2009 Posts: 9 Location: Singapore
|
Posted: Fri Feb 27, 2009 2:45 pm Post subject: HelloWorldAlmost.cpp |
|
|
I had two problems with this file and I was wondering if all compilers have the same result (I use Visual Studio 2008 trial). From my understanding, a cpp file including a STL class (eg #include <list>) and compiling/running with VS2008 should also compile/run with another compiler on linux for example.
First, one compile error is C2228 because the list<double> myList is created with the default constructor, giving a size to the constructor:
list<double> myList(myVec.size());
remove this compiled error. If we initialized with a size less than myVec.size() we have a runtime error.
Second, one assert runtime error: the merge function expects sorted vector:
sort(myVec3.begin(), myVec3.end());
sort(myVec4.begin(), myVec4.end());
merge(myVec3.begin(), myVec3.end(), myVec4.begin(), myVec4.end(),
myVec5.begin());
|
|
| Back to top |
|
 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 462 Location: Amsterdam, the Netherlands
|
Posted: Mon Mar 12, 2007 9:02 am Post subject: Chapter 11 |
|
|
| Exercises and Answers
|
|
| Back to top |
|
 |
|