enum code

hi,
I am encountering compilation error due to enum....here's the relevant code in a class Options:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Options
{
public:
enum Type{Put=-1,None=0,Call=1};
Options(double volatility, Type type):volatility_(vol),type_(type)
{}
private:
double volatility_; Type type_;
};
class CreateOptions
{
public:
CreateOptions():volatility_(Null<Real>()),type_(Options::None)
{}
CreateOptions& vola(const double& vol)
{
volatility_=vol;
return *this;
}
CreateOptions& kind(const Options::Type& type)
{
type_=type;
return *this;
}
};
void test()
{
double vol=0.11;
Options::Type type(Options::Call);
Options optiontest=CreateOptions().kind(type).vola(vol);
}

int main(int argc, char *argv[])
{
test();
return 0;
}


I get the following error:
1
2
3
4
5
6
7
option.cpp: In constructor ‘CreateOptions::CreateOptions()’:
option.cpp:31:57: error: class ‘CreateOptions’ does not have any field named ‘type_’
option.cpp: In member function ‘CreateOptions& CreateOptions::kind(const Options::Type&)’:
option.cpp:73:5: error: ‘type_’ was not declared in this scope
option.cpp: In member function ‘CreateOptions::operator Options() const’:
option.cpp:80:5: error: ‘type_’ was not declared in this scope
option.cpp:81:31: error: ‘type_’ was not declared in this scope

any help would be appreciated...thx!
Last edited on
With the proper formatting of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Options
{
public:
	enum Type{Put=-1,None=0,Call=1};
	Options(double volatility, Type type):volatility_(vol),type_(type){}
private:
	double volatility_; Type type_;
};

class CreateOptions
{
public:
	CreateOptions():volatility_(Null<Real>()),type_(Options::None){}
	CreateOptions& vola(const double& vol)
	{
		volatility_=vol;
		return *this;
	}
	CreateOptions& kind(const Options::Type& type)
	{
		type_=type;
		return *this;
	}
};

void test()
{
	double vol=0.11;
	Options::Type type(Options::Call);
	Options optiontest=CreateOptions().kind(type).vola(vol);
}

int main(int argc, char *argv[])
{
	test();
	return 0;
}
It is clear that you have one class trying to access stuff from another class! Classes can't know about each other in the way you are trying to do.
hi L B,
thx for your response!
In that case, what would you suggest be the best way to do this.
Merge the two classes. Is there a reason you need them separate?
Last edited on
is merging them better or should I make one a derivative of another....I am basically trying to write an option pricer (I have listed only 2 parameters, type and volatility, for simplicity sake) .... was going through an example that suggested 2 separate classes, so I was trying to test it out that way....
What example suggested two separate classes? I think you're misunderstanding what to do and how to do it, as this seems to be a hundred times simpler in one class...

on this site:
http://quantlib.org/docs.shtml
under the
QuantLib introduction, part I , it starts on page 43 on the pdf file..

In case I've misunderstood, I'd like to correct myself...
Last edited on
You can't see it because it gets cut off of the bottom of the page, but luckily a copy-paste was able to reveal this invisible code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class MakeMyOption {
public :
MakeMyOption (): mat_ ( Null < Real >()) , vol_ ( Null < Real >()) , spot_ ( Null < Real >()) ,
strike_ ( Null < Real >()) , forRate_ ( Null < Real >()) ,
domRate_ ( Null < Real >()) , type_ ( MyOption :: None ){
}
MakeMyOption & withMat ( const double & mat ){
mat_ = mat ;
return * this ;
}
MakeMyOption & withVol ( const double & vol ){
vol_ = vol ;
return * this ;
}
MakeMyOption & withSpot ( const double & spot ){
spot_ = spot ;
return * this ;
}
MakeMyOption & withStrike ( const double & strike ){
strike_ = strike ;
return * this ;
}
MakeMyOption & withForRate ( const double & forRate ){
forRate_ = forRate ;
return * this ;
}
MakeMyOption & withDomRate ( const double & domRate ){
domRate_ = domRate ;
return * this ;
}
MakeMyOption & withType ( const MyOption :: Type & type ){
type_ = type ;
return * this ;
}
operator MyOption () const {
QL_REQUIRE ( mat_ != Null < Real >() , " Maturity not set ! " );
// Check other parameters too
QL_REQUIRE ( type_ != MyOption :: None , " Option type not set ! " );
return MyOption ( mat_ , vol_ , type_ , spot_ , forRate_ , domRate_ , strike_ );
}
Time getMat () const { return mat_ ;}
Real getSpot () const { return spot_ ;}
Real getStrike () const { return strike_ ;}
private :
Time mat_ ;
Volatility vol_ ;
Real spot_ , strike_ ;
Rate forRate_ , domRate_ ;
MyOption :: Type type_ ;
};
You can see that the members are here in this class, and not the other class. I believe that this was a misunderstanding due to a poorly laid out PDF.
Last edited on
thx very much L B,
I was missing the private MyOption :: Type type_ ; in my code, and after adding that, it works like a charm....appreciate your help :-)
btw, what's a factory class? I'm still familiarizing myself with QuantLib, so maybe this is a very basic question....
Topic archived. No new replies allowed.