Why there is always a compilation error like "expected a ;"?

see the following code

I defined two functions, one is inside the other:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool clusterLabelling() {

	vector<tIndex_t> unlabelT;
	vector<pIndex_t> unlabelP;
	vector<tIndex_t>  searchT;
	vector<pIndex_t>  searchP;
	vector<tIndex_t>  searchTback;
	vector<pIndex_t>  searchPback;

	//a function to determine if the index of throats ever searched


	bool inTback(const tIndex_t index) { //error messagehere: expected a ";"
	    for(vector<tIndex_t>::size_type i=0; i<searchTback.size(); i++) {
			if(index==searchTback[i]) return true;
	    }
	    return false;
	}

...
}


I didn't see any possible problem there need a ; and I didn't find any other places a ; is missed. What's the problem?
a function definition cannot appear inside another function, but a function declaration can. Inside the function bool clusterLabelling(), after bool inTback(const tIndex_t index), the only thing that can follow is the semicolon (which would make that line a function declaration).
You can use lambda expressions that to define functions on the fly. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool clusterLabelling() {

	vector<tIndex_t> unlabelT;
	vector<pIndex_t> unlabelP;
	vector<tIndex_t>  searchT;
	vector<pIndex_t>  searchP;
	vector<tIndex_t>  searchTback;
	vector<pIndex_t>  searchPback;

	//a function to determine if the index of throats ever searched


	auto inTback = [&](const tIndex_t &index) -> bool 
	{
	    for(vector<tIndex_t>::size_type i=0; i<searchTback.size(); i++) {
			if(index==searchTback[i]) return true;
	    }
	    return false;
	};

...
}
Last edited on
Thanks vlad and cubbi, but vlad's solution looks a little strange to me yet. I am going to declare the function inside the function clusterLabeling() and define it after it. Because I need to use datas from clusterLabeling().

But it seems my idea don't work, compilation error message shows all the

1
2
3
4
5
6
	vector<tIndex_t> unlabelT;
	vector<pIndex_t> unlabelP;
	vector<tIndex_t>  searchT;
	vector<pIndex_t>  searchP;
	vector<tIndex_t>  searchTback;
	vector<pIndex_t>  searchPback;


is undefined to the sub function inTback(); How should I arrange these items?
Honestly, if you want to do it that way then vlad's solution is good. Otherwise, you could pass the vector(s) you are referring to as parameters to that function.
I am just not familiar with vlad's syntax. Maybe I'll define the variable and function all out of function body. Is there other better methods?
Actually, I'd say it's a perfect opportunity to learn about it. Look up "lambda functions" in relation to C++ and read a few tutorials about them.
Topic archived. No new replies allowed.