Quantcast

Error in compiling of QuantLib examples

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Error in compiling of QuantLib examples

ziegele
Hello,

I am a starter of using QuantLib. I'm currently following the tutorial as shown in http://cogitolearning.co.uk/?p=332

The code I wrote in enclosed in attachment.main.cpp

If I follow exactly as what's shown in the tutorial, I got an error when compiling:


Then I figured out that there might be a typo in the tutorial, so I changed cashFlows[i].accept(addCashFlowVisitor); to cashFlows[i]->accept(addCashFlowVisitor);

However, I still got an error when trying to compile:


I'm wondering how to fix it?

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

Yannis
Hi,

I think the problem lies with the line:
cashFlows.emplace_back(SimpleCashFlow(2.0, d1));
The elements in CashFlows are shared_ptr. Therefore you should pass an argument to emplace_back out of which a shared_ptr may be constructed.
So change the above line to: 
cashFlows.emplace_back(new SimpleCashFlow(2.0, d1));
Alternatively use 
cashFlows.push_back( boost::shared_ptr<CashFlow>(new SimpleCashFlow(2.0, d1)) );
Good luck,

Yannis


ziegele <[hidden email]> schrieb am 22:55 Dienstag, 24.Januar 2017:


Hello,

I am a starter of using QuantLib. I'm currently following the tutorial as
shown in  http://cogitolearning.co.uk/?p=332
<http://http://cogitolearning.co.uk/?p=332

The code I wrote in enclosed in attachment. main.cpp
<http://quantlib.10058.n7.nabble.com/file/n17991/main.cpp

If I follow exactly as what's shown in the tutorial, I got an error when
compiling:
<http://quantlib.10058.n7.nabble.com/file/n17991/error1.jpg>

Then I figured out that there might be a typo in the tutorial, so I changed
cashFlows[i].accept(addCashFlowVisitor); to
cashFlows[i]->accept(addCashFlowVisitor);

However, I still got an error when trying to compile:
<http://quantlib.10058.n7.nabble.com/file/n17991/error2.jpg>

I'm wondering how to fix it?

Thanks



--
View this message in context: http://quantlib.10058.n7.nabble.com/Error-in-compiling-of-QuantLib-examples-tp17991.html
Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

ziegele
Thanks Yannis! It indeed fixes this error.

However, if I try to assign different types of inputs into the cashFlows vector:



I got SimpleCashFlow and Redemption calculated correctly, but AmortizingPayment didn't calculate:



Do you know why? Complete source code is also attached.

main.cpp

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

Yannis
You 're welcome!

Add the following code defining the default constructor inside the AddCashFlowAmounts class:

AddCashFlowAmounts() : cashFlowSum(0), simpleCashFlowSum(0), redemptionSum(0), amortizingPaymentSum(0) {}

This should initialize the variables to 0 when you call AddCashFlowAmounts addCashFlowsVisitor; inside the calcCashFlowSums function.

Regards.
Ioannis


ziegele <[hidden email]> schrieb am 16:11 Mittwoch, 25.Januar 2017:


Thanks Yannis! It indeed fixes this error.

However, if I try to assign different types of inputs into the cashFlows
vector:

<http://quantlib.10058.n7.nabble.com/file/n17994/input.jpg>

I got SimpleCashFlow and Redemption calculated correctly, but
AmortizingPayment didn't calculate:

<http://quantlib.10058.n7.nabble.com/file/n17994/output.jpg>

Do you know why? Complete source code is also attached.

main.cpp <http://quantlib.10058.n7.nabble.com/file/n17994/main.cpp

Thanks.



--
View this message in context: http://quantlib.10058.n7.nabble.com/Error-in-compiling-of-QuantLib-examples-tp17991p17994.html

Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

ziegele
Thanks Yannis!

1 last question: is there a way to emplace_back a CashFlow shared_ptr? It shows that it's purely virtual, which only serves as an interface:



Is there anyway of generating a non-zero report for "all other cash flows" part?



Thanks,
ziegele
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

Ioannis Rigopoulos

You 're welcome Ziegele,

1) No, because CashFlow is abstract and emplace_back relies on the existence of a constructor.

2) I am not familiar with the visitor pattern, but I suspect you could achieve this by removing all the visit overloads except the void visit( CashFlow & c ) one.

Then you modify the code inside as follows:

void visit( CashFlow & c ) {

  cashFlowSum += p->amount();

    if( SimpleCashFlow * p = dynamic_cast<CashFlow *>( &c ) ) {

	simpleCashFlowSum += p->amount();

    }

    else if( Redemption * p = dynamic_cast<Redemption *>( &c ) ) {

	redemptionSum += p->amount();

    }

    else if( AmortizingPayment * p = dynamic_cast<AmortizingPayment *>( &c ) ) {

	amortizingPaymentSum += p->amount();

    }

}


Let me know if this works

Cheers

Yannis

On 26.01.2017 20:42, ziegele wrote:
Thanks Yannis!

1 last question: is there a way to emplace_back a CashFlow shared_ptr? It
shows that it's purely virtual, which only serves as an interface:

<http://quantlib.10058.n7.nabble.com/file/n18024/cashFlow.jpg> 

Is there anyway of generating a non-zero report for "all other cash flows"
part?

<http://quantlib.10058.n7.nabble.com/file/n18024/output.jpg> 

Thanks,
ziegele



--
View this message in context: http://quantlib.10058.n7.nabble.com/Error-in-compiling-of-QuantLib-examples-tp17991p18024.html
Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users





Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Error in compiling of QuantLib examples

Luigi Ballabio
Your visitor should work as you've written it.  Try adding some other kind of cash flow, such as FixedRateCoupon.

Luigi




On Thu, Jan 26, 2017 at 10:55 PM Ioannis Rigopoulos <[hidden email]> wrote:

You 're welcome Ziegele,

1) No, because CashFlow is abstract and emplace_back relies on the existence of a constructor.

2) I am not familiar with the visitor pattern, but I suspect you could achieve this by removing all the visit overloads except the void visit( CashFlow & c ) one.

Then you modify the code inside as follows:

void visit( CashFlow & c ) {

  cashFlowSum += p->amount();

    if( SimpleCashFlow * p = dynamic_cast<CashFlow *>( &c ) ) {

	simpleCashFlowSum += p->amount();

    }

    else if( Redemption * p = dynamic_cast<Redemption *>( &c ) ) {

	redemptionSum += p->amount();

    }

    else if( AmortizingPayment * p = dynamic_cast<AmortizingPayment *>( &c ) ) {

	amortizingPaymentSum += p->amount();

    }

}


Let me know if this works

Cheers

Yannis

On 26.01.2017 20:42, ziegele wrote:
Thanks Yannis!

1 last question: is there a way to emplace_back a CashFlow shared_ptr? It
shows that it's purely virtual, which only serves as an interface:

<http://quantlib.10058.n7.nabble.com/file/n18024/cashFlow.jpg> 

Is there anyway of generating a non-zero report for "all other cash flows"
part?

<http://quantlib.10058.n7.nabble.com/file/n18024/output.jpg> 

Thanks,
ziegele



--
View this message in context: http://quantlib.10058.n7.nabble.com/Error-in-compiling-of-QuantLib-examples-tp17991p18024.html
Sent from the quantlib-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users





Avast logo

This email has been checked for viruses by Avast antivirus software.
www.avast.com


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Loading...