Initializer

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)

#include <iostream>
#include <fstream>
#include <cmath>
#include "subs.h"

using namespace 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= new double[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];
return 0;
}
Can you give me the rest of the code?
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

double randomwalk(int seed, int N, double steplength) {
//random number generator
unsigned int c(16807);
unsigned int p(21474837);
unsigned int *r;
double *randomnumb;
r= new unsigned int[N+1];
randomnumb=new double [N];
r[0]=seed;
randomnumb[0]=r[0];
randomnumb[0]=randomnumb[0]/p;
//generate random numbers
for(int i=1;i<N+1;++i) {
r[i]=r[i-1]*c%p;
randomnumb[i-1]=r[i];
randomnumb[i-1]=randomnumb[i-1]*2*3.14159265/p;
}
//x and y components of each vector
double *x,*y;
x= new double [N];
y= new double [N];
for(int i=0;i<N;++i) {
x[i]=cos(randomnumb[i])*steplength;
y[i]=sin(randomnumb[i])*steplength;
}
//measure the end to end vector
double Rx(0),Ry(0);
for(int i=0;i<N;++i) {
Rx=Rx+x[i];
Ry=Ry+y[i];
}
double R2;
R2=Rx*Rx+Ry*Ry;
delete[] r;
delete[] randomnumb;
delete[] x;
delete[] y;
return R2;
}


#ifndef SUBS_H_INCLUDED
#define SUBS_H_INCLUDED

double randomwalk(int seed, int N, double steplength)

#endif // SUBS_H_INCLUDED


And the header


Sorry, the last line "and the header" should be above:

#ifndef SUBS_H_INCLUDED
#define SUBS_H_INCLUDED

int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks


These values, Right?
What is with these values?

The error message is: line 6 error: expected initializer before using

There is some problem with an initializer, but I don't know what this means.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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);

	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;
}



now work
correct me if I'm wrong but you have 3 files

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
#include <cmath>
#include "subs.h"

using namespace 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>

using namespace 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"

using namespace 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 .
Last edited on
This is what I compiled on my computer and it worked fine:
If you still get a error with this, it's not in the code.

What is the command you use to compile and link?

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "randomwalk.h"
using namespace 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= new double[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_ */ 


randomwalk.cpp
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
#include <iostream>
#include <fstream>
#include <cmath>
#include "randomwalk.h"

double randomwalk(int seed, int N, double steplength) {
//random number generator
unsigned int c(16807);
unsigned int p(21474837);
unsigned int *r;
double *randomnumb;
r= new unsigned int[N+1];
randomnumb=new double [N];
r[0]=seed;
randomnumb[0]=r[0];
randomnumb[0]=randomnumb[0]/p;
//generate random numbers
for(int i=1;i<N+1;++i) {
r[i]=r[i-1]*c%p;
randomnumb[i-1]=r[i];
randomnumb[i-1]=randomnumb[i-1]*2*3.14159265/p;
}
//x and y components of each vector
double *x,*y;
x= new double [N];
y= new double [N];
for(int i=0;i<N;++i) {
x[i]=cos(randomnumb[i])*steplength;
y[i]=sin(randomnumb[i])*steplength;
}
//measure the end to end vector
double Rx(0),Ry(0);
for(int i=0;i<N;++i) {
Rx=Rx+x[i];
Ry=Ry+y[i];
}
double R2;
R2=Rx*Rx+Ry*Ry;
delete[] r;
delete[] randomnumb;
delete[] x;
delete[] y;
return R2;
}
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.
As I was saying, the file names don't matter as long as your #includes are correct.

main.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "subs.h"
using namespace std;

int main()
{
 ...
}


subs.h
1
2
3
4
5
6
#ifndef RANDOMWALK_H_
#define RANDOMWALK_H_

double randomwalk(int seed, int N, double steplength);

#endif /* RANDOMWALK_H_ */  


randomwalk.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>
#include <cmath>
#include "subs.h"

double randomwalk(int seed, int N, double steplength)
{
   ...
}


will work fine too.
Topic archived. No new replies allowed.