Datasim Financial Forums Forum Index


 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Chapter 11

 
Post new topic   Reply to topic    Datasim Financial Forums Forum Index -> Introduction to C++ for Financial Engineers (Duffy)
View previous topic :: View next topic  
Author Chapter 11
Cuchulainn



Joined: 18 Dec 2006
Posts: 462
Location: Amsterdam, the Netherlands

PostPosted: Wed Dec 30, 2009 9:39 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Cuchulainn



Joined: 18 Dec 2006
Posts: 462
Location: Amsterdam, the Netherlands

PostPosted: Sun Mar 01, 2009 2:31 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
pierrechristophe



Joined: 20 Feb 2009
Posts: 9
Location: Singapore

PostPosted: Sun Mar 01, 2009 10:52 am    Post subject: Original and modified file Reply with quote

Hello,

Please find both files



HelloWorldAlmost.cpp
 Description:
Original file on the CD

Download
 Filename:  HelloWorldAlmost.cpp
 Filesize:  3.9 KB
 Downloaded:  112 Time(s)


HelloWorldAlmost_Modified.cpp
 Description:
Modified file

Download
 Filename:  HelloWorldAlmost_Modified.cpp
 Filesize:  4.25 KB
 Downloaded:  107 Time(s)

Back to top
View user's profile Send private message
Cuchulainn



Joined: 18 Dec 2006
Posts: 462
Location: Amsterdam, the Netherlands

PostPosted: Fri Feb 27, 2009 2:51 pm    Post subject: Reply with quote

Please post the full code and we can compile it. Thanks.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
pierrechristophe



Joined: 20 Feb 2009
Posts: 9
Location: Singapore

PostPosted: Fri Feb 27, 2009 2:45 pm    Post subject: HelloWorldAlmost.cpp Reply with quote

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
View user's profile Send private message
Cuchulainn



Joined: 18 Dec 2006
Posts: 462
Location: Amsterdam, the Netherlands

PostPosted: Mon Mar 12, 2007 9:02 am    Post subject: Chapter 11 Reply with quote

Exercises and Answers
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Datasim Financial Forums Forum Index -> Introduction to C++ for Financial Engineers (Duffy) All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group