function-definition


I am trying to run a program and have defined the following function in main()..the relevant code is below.
1
2
3
4
5
6
7
8
9
10
11
12
 istream& read_work(istream& in, vector<double>& sp)
  {
    if(in)
      {
	sp.clear();
	double x;
	while(in>>x)
	  sp.push_back(x);
	in.clear();
      }
    return in;
  }

I get this error on compilation:
a function-definition is not allowed here before ‘{’ token
expected ‘}’ at end of input

I have the following function definition before main()
 
istream& read_work(istream&, vector<double>&);


I presume my function definition is not correct - how do I fix it?
thank you in advance!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

using namespace std;

istream & read_work(istream& in, vector<double>& sp){
	if(in){
		sp.clear();
		double x;
		while(in>>x){
	  		sp.push_back(x);
	  	}
		in.clear();
	}
	return in;
}

int main(){
	cout << "It works 8]!" << endl;
	return 0;
}


This code compiled and worked for me so it might be something else
You can't define functions inside other functions in C++. You need to move your function outside of main.
that was my silly error....thanks for pointing it out...it worked.
Topic archived. No new replies allowed.