I have a working cpp file, lets just say it performs a+b, the filename is abc.cpp. I want to call up that file, function, in another file without having to rewrite the code and simply call up the function in another cpp file, xyz.cpp and simply map my variables over to the abc.cpp file.

I'm from hardware end of town, and what I woudl do is create a package and call up the package and simply instantiate the package name and perform a port map.

How do I perform somethign similar in c++?

Thanks

Member Avatar for iamthwee

just do a #include "abc.cpp" in your original program (xyz.cpp) i think, as long as you take out your int main from abc.cpp?

just do a #include "abc.cpp" in your original program (xyz.cpp) i think, as long as you take out your int main from abc.cpp?

I remove int main in the file I want to reference, and I get the following error:

black_schole.cpp(16) : error C2470: 'black_schole' : looks like a function definition, but there is no parameter list; skipping apparent body

Also, do I need a .h file?

my function has 7 inputs and 2 outputs, I was to pass the inputs from my top levels to this function, then pass back the result. How in the world is this done in C++? I can do this all day long in VHDL/Verilog, but C++ is a different beast.

Member Avatar for iamthwee

What compiler and OS?

If you have declared classes then it is usually a good idea to use a .h file and separate out the definition and declaration of the class. As far as the error is concerned can you post some code here? the main function and abc.cpp will do.

Below is my function:

#include <stdafx.h>
#include <iostream>
#include <math.h>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <gsl\gsl_randist.h>
#include <gsl\gsl_cdf.h>
#include <string>

using namespace std;

int black_schole()
{

	//BS variables
	double s, k, d1, d2, v, T, r, OC, OP; // s=stock price, k=strike price, r=rf, v=volatility, t = days until expiration, T = conversion, O = price of option
	
	//Black Schole code START:
	d1 = (log(s/k) + ((r + v*v*.5))*T)/(v*sqrt(T)); //calculation of d1
	d2 = d1 - v*sqrt(T); //calculation of d2
	OC = s*gsl_cdf_ugaussian_P(d1) -k*(exp(-r*T))*gsl_cdf_ugaussian_P(d2); //gsl_cdf_ugaussian converts from Zo to PHI, probability within standard norm dist
	
	//put call parity
	OP = k*exp(-r*T) + OC - s;
	
return 0;

}


Below is the top level file I want to pass I/O to the above file:

#include <stdafx.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
//#include <black_schole.h>
#include <black_schole.cpp>

using namespace std; 

 int main()
 
 {
 
	double xh, xl, m;
	double epsilon;

	double s, k, d1, d2, v, T, r, OC, OP;

	cout <<"Enter a low limit: ";
	cin >> xl;
	cout <<"Enter a high limit: ";
	cin >> xh;
	cout <<"Enter a value for epsilon: ";
	cin >> epsilon;
 
	std::cout << "Please enter in the risk free rate: " ;
	std::cin >> r;
	std::cout << "Please enter in the stock price: " ;
	std::cin >> s;
	std::cout << "Please enter in the strike price: " ;
	std::cin >> k;
	std::cout << "Please enter in the stocks volatility: " ;
	std::cin >> v;
	std::cout << "Please enter in the time to maturity, in years: " ;
	std::cin >> T;

	//black_schole();
	cout << OC << endl;
	double i;
	while ((xh - xl) > epsilon)
 	for (i=0; i=1000; i++)
	{
		m = (xh + xl) / 2;

			if (xl*m < 0)
				xh = m;
			else if (xh*m < 0)
				xl = m;
			else
				xh = m;
			break;
	}
	

 cout << xh << endl; 

 system ("pause");

 return 0;

 }

What compiler and OS?

MSFT Visual C++ 2008 express

iamthwee, thanks, when I get this working I'll move over to dev cpp

Member Avatar for iamthwee

Sorry can't help mate. I only have dev-cpp here.

Good luck.

First this is to learn about using Code Tags (CODE) option on your reply box. Read more here.

Your file is probably not getting included because of using #include <black_..>. If it is in the same directory as the main file use #include "black..".

Even then you will get a linker error if you compile n link both the files. Try compiling only main.cpp or put the function in the main file itself.

Thanks - I can put the entire function in my project, however, I want to reuse that file over and over again to call up as an include file in multiple projects withotu having to paste the function in. There must be a way to perform a function call, that exists in another file, into a different file without having to reinvent or paste the wheel everytime. Consider my one file an operator I want to call up anywhere.

hi beaverkill,

I am just using MS Visual Studio 2008 (professional edition). Assuming that express edition doesn't differ from my edition that much, it is easy to include header files from directories other then your current project dir. I have a foreign language Visual studio here, I try to translate the steps into English:

1. Click Project and open the properties of your current project (ALT F7)

2. Open sub tree C/C++. Here you must select the first Entry named "Common settings" or something like that. This will show items like Warning level, debug information, Unicode etc. on the right-hand window which you can change.

3. The first Label, the first line, in this window is something like "Additional Include Directory". There you simply fill in the full path to the directory where your header file is located. For example: Headerfiles I often include in various projects are all centralized in directory d:\numeric\h. I only need to fill in this complete path in the box right-hand of the Label "Additional Include Directory".

4. Then I add #include "Matrices.h", which is located in d:\numeric\h, to the main cpp file and the class, functions, variables etc. of this distant header file are now accessible within my current project which is located elsewhere.


-- tesu

Addition to my prior posting (I am not further allowed to edit it for time seems to be expired):

For some special issues I also run wxDev-C++ 7.3.1.3 and code::Blocks 10.05. Both are really easily to handle but absolutely powerful. Because I also run it on Unix I myself now prefer code::blocks (the older version was awful). So it is a true relaxation working with them instead of getting somehow teased by using the Visual Studio.

-- tesu

Thanks - I can put the entire function in my project, however, I want to reuse that file over and over again to call up as an include file in multiple projects withotu having to paste the function in. There must be a way to perform a function call, that exists in another file, into a different file without having to reinvent or paste the wheel everytime. Consider my one file an operator I want to call up anywhere.

Did you try the other stuff mentioned in my post? the last part was just an alternative. Did you try include using the "" and compiling only main.cpp?

All you have to do is include the .cpp file

#include "abc.cpp"

and compile only the .cpp with the main().

You need to use "" because the, <>, are telling the compiler the included files are
in the main compiler library where all the headers are located such as the string, iostream, fstream and so on. With the "", it tells the compiler that the file is in the same directory as the .cpp file with the main(). Honestly, switch to Linux and compile if there. I have used all sorts of IDE's on windows, which i did use them to compile, but nothing compares to the simplicity of compiling in linux, using the command in the shell

gcc foo.cpp -o foo

.
I think people rely on IDE's to much. This is just my own opinion thought.

Sorry I meant to put

g++ foo.cpp -o foo

gcc is if you are using just the c compiler not the c++ compiler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.