I have not a lot of experience with C++. So far I did everything in one file. Now I tried to use 2 source files and 1 header.
I get the message that an initializer is expected.
This is the code of the mein source file (where the error is reported)
int main()
{
int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks
double steplength(1); //length of one step
double *R;
R= new double[N2];
//initial seed
int seed (11324);
int main()
{
int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks
double steplength(1); //length of one step
double *R;
R= newdouble[N2];
//initial seed
int seed (11324);
for(int i=0;i<N2;++i) {
R[i]=randomwalk(seed,N,steplength);
++seed;
}
cout <<R[23];
delete [R]; // delete []R; change it here
return 0;
}
#include <iostream>
#include <fstream>
#include <cmath>
#include "subs.h"
usingnamespace std;
int main()
{
/* your code*/
}
subs.h
1 2 3 4 5 6
#ifndef SUBS_H_INCLUDED
#define SUBS_H_INCLUDED
double randomwalk(int seed, int N, double steplength)
#endif // SUBS_H_INCLUDED
subs.cpp
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
double randomwalk(int seed, int N, double steplength)
{
/* your code*/
}
If this is what you mean your on the right track, but you need to understand how compiling works.
To put it in a simple way, the compiler will compile every cpp file separately first, doing a #include "something.h" will tell the compiler that some thing you call will be found in a other file (in this case something.h. But at this point the compiler has no clue where the code is only what will be in it. After this the linker will combine all these files to one binary. In your case the linker can't find where the actual code for subs.h is (sins header and source files don't need to have the same name or even be in the same location) This is why a source file needs to include it's own header file to let the linker know what header file is linked to what source file. So you only need to add #include "subs.h" to the top of your subs.cpp file:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <fstream>
#include <cmath>
#include "subs.h"
usingnamespace std;
double randomwalk(int seed, int N, double steplength)
{
/* your code*/
}
And since you are using header safeguards the actual source will only be inserted ones (upon the first include) in the final binary.
(In a actual compiler and more advanced stuff it's a bit more complicated then this, and the compiler/linker does many more things. But that's stuff you don't touch unless you know what you are doing.)
Ok, I added #include "subs.h" into my randomwalk.cpp file. I still get an error "expected initializer before double". It is in the randomwalk.cpp file. (6. Line)
To your first question, yes I have 3 files, only that the file subs.cpp is called randomwalk.cpp .
#include <iostream>
#include "randomwalk.h"
usingnamespace std;
int main()
{
int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks
double steplength(1); //length of one step
double *R;
R= newdouble[N2];
//initial seed
int seed (11324);
for(int i=0;i<N2;++i) {
R[i]=randomwalk(seed,N,steplength);
++seed;
}
cout <<R[23];
delete []R; // delete []R; change it here
return 0;
}
randomwalk.h
1 2 3 4 5 6
#ifndef RANDOMWALK_H_
#define RANDOMWALK_H_
double randomwalk(int seed, int N, double steplength);
#endif /* RANDOMWALK_H_ */
The file names were main.cpp, subs.h, randomwalk.cpp. Thanks for your help. It works when I copy your code (and change the difference in the file name).
I don't know where my mistake war, but thanks for the help it works now.