-3

I am a newbie at C++ and was given this assignment. I have to use this header file, from which I have provided the relevant excerpt. The class name is PayoffAsianCall.

PayoffAsianCall(double strike);
virtual double operator()(std::vector<double> & spot) const;
virtual ~PayoffAsianCall(){}

I want to assign some stuff to the operator in my main file I have been using

    PayoffAsianCall &PayoffAsianCall::virtual double operator()(std::vector<double> & spot) const { do stuff here}

but this has not been working. Can someone explain to me the correct syntax please?

  • [This should be covered early in any non-fraudulent C++ text.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Sep 07 '17 at 05:37
  • Welcome to Stack Overflow! It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight Sep 07 '17 at 12:09

1 Answers1

0

It´s always hard to gain some ground for beginners and c++ isn´t that easy to tame. So here are some hints for you:

We start with your try

PayoffAsianCall &PayoffAsianCall::virtual double operator()(std::vector & spot) const { do stuff here}

First: The first thing to note is the return type, you say the return type is

PayoffAsianCall &

btw. you should really pay attention to the position of the &. In this case it means referenece and its PayoffAsianCall Reference so it should have been PayoffAsianCall& and not PayoffAsianCall &.

But anyway: in the class definition you say the return value is double so it should be double:

double PayoffAsianCall::virtual double operator()(std::vector & spot) const { do stuff here}

thats already much better. Now we need the operator name, since it is a class member the name includes the class name virtual and double have nothing to do with the name, virtual is reserved for use in the class definition itself so there is no room for it here and double is already taken care of. So we now have:

double PayoffAsianCall::operator()(std::vector & spot) const { do stuff here}

That´s all. I would recommend, as mentioned before, to correctly place the &. It´s not a std::vector reference-spot but a std::vector-reference spot:

double PayoffAsianCall::operator()(std::vector& spot) const { do stuff here}

done.

DrSvanHay
  • 1,170
  • 6
  • 16
  • Thank you, that was very useful. I am a graduate student and for some reason, the professors think that it is productive to just have people try to figure this out using Google. I'd also appreciate a recommendation for a good reference text. Literally started learning C++ around 24 hours ago. Thanks Again! – AliceinWonderLand Sep 07 '17 at 08:07
  • @AliceinWonderLand If the answer was useful please do not forget to accept the answer. A book recommendation is not as easy as it sounds but my bet would be "Programming: Principles and Practice Using C++" by the creator of c++ Bjarne Stroustrup. The first half is easy stuff but pace increases fast. So its suited to get a start as well as to get some more advanced questions answered after you gained some experience. – DrSvanHay Sep 07 '17 at 08:23