• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Weird C++/ VS 6.0 behaviour

Joined
4/5/08
Messages
267
Points
38
I was trying to create a class named BlackScholes.

I created a .h file "BlackScholes.h" and started the file with

#ifndef BLACKSCHOLES_H
#define BLACKSCHOLES_H

class BlackScholes{
...
...


};

...
...

#endif

This did not create the classes as desired.

However after 2 hours long of debugging I changed my filename to BlackScholes1.h and

#ifndef BS_H
#define BS_H

#endif


This is really weird.

Does anyone know the reason for this ?
 
It's not clear what the problem or the solution are. What do you mean by "This did not create the classes as desired." Compiler errors? Which exactly? What was the outcome of the exercise?
 
Hi,
First, it is better to call the file .hpp (VC++ thinks .h is C, maybe).
Second VC++ 6. is real old; you can download Visual Studio C++ 2008 for FREE and it's very good.

I have some source examples and so on at www.datasimfinancial.com

HTH
 
Aditya perhaps there already was a macro for

#ifndef BLACKSCHOLES_H
#define BLACKSCHOLES_H

If that macro is executed in another header file then it seems to me that both wouldn't load.
 
It's not clear what the problem or the solution are. What do you mean by "This did not create the classes as desired." Compiler errors? Which exactly? What was the outcome of the exercise?

On the left pane, we have the class view, so when you click there you find that the classes are actually not declared.
 
Could it be because of the way the whole project/file mechanisms work?

This is why I don't use VS, but Code::Blocks. That whole project/file stuff is just a giant PITA for me >.<...
 
Sorry if this is well known to you but I assume you said "Add->New Item->Header File" or "Add->Existing Item". Just dumping the .h into the directory won't work even though you can see it if you select "Show All Files".
 
Just to check -- you didn't also put the #ifndef lines into the .cpp file by accident, did you? That will cause those problems.

The left pane is a little slow to update, I notice. I would be more concerned with compile errors you get.
 
Just to check -- you didn't also put the #ifndef lines into the .cpp file by accident, did you? That will cause those problems.

The left pane is a little slow to update, I notice. I would be more concerned with compile errors you get.

Everything was fine. After I changed just the two names it worked flawlessly.

If I remember it correct it said something like "No such label found" or "Cannot find type: BlackScholesCall "

Where BlackScholesCall is inherited from BlackScholes.
 
why not post the complete code so we can compile it first-hand?

incidentally, I tried to change my file and macro names back to those were causing the problem and found out that it had no issues now.

However here is the code

.h file

C++:
#ifndef BLACKSCHOLES_H
#define BLACKSCHOLES_H

#include <iostream>

using namespace std;

class BlackScholes
{
public:
    
    //Constructors
    BlackScholes(){};
    BlackScholes(double r_,double q_,double T_,double sig_);

    
    //Destructor
    virtual ~BlackScholes(){};

    virtual BlackScholes* clone() const =0;

private:
    double r;
    double q;
    double T;
    double sig;
};


//Inherit BlackScholesCall from BlackScholes
class BlackScholesCall : public BlackScholes
{
public:
    BlackScholesCall(double Strike_,double r_,double q_,double T_,double sig_);

    
    virtual ~BlackScholesCall(){};
    virtual BlackScholes* clone()const;

private:
    double K;
};


//Inherit BlackScholesPut from BlackScholes
class BlackScholesPut : public BlackScholes
{
public:
    BlackScholesPut(double Strike_,double r_,double q_,double T,double sig_);

    
    virtual ~BlackScholesPut(){};
    virtual BlackScholes* clone()const;

private:
    double K;
};


#endif
.cpp file


C++:
#include "BlackScholes.h"


BlackScholes::BlackScholes(double r_, double q_, double T_, double sig_){
    r = r_;
    q = q_;
    T = T_;
    sig = sig_;
}

    


//BLACK SCHOLES CALL


BlackScholesCall::BlackScholesCall(double Strike_,double r_,double q_,double T_,double sig_)
:BlackScholes(r_,q_,T_,sig_){
    K = Strike_;
}

BlackScholes* BlackScholesCall::clone() const
{
    return new BlackScholesCall(*this);
}


//BLACKSCHOLES PUT


BlackScholesPut::BlackScholesPut(double Strike_,double r_,double q_,double T_,double sig_)
:BlackScholes(r_,q_,T_,sig_){
    K = Strike_;
}


BlackScholes* BlackScholesPut::clone() const
{
    return new BlackScholesPut(*this);
}
 
Back
Top