function-definition is not allowed here before '{'.

Why am i getting this error if i clearly stated it before main? (located by comment that says perform a binary search) - line 78

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

constexpr int SIZE{ 200 };

int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);



int main()
{

    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    int numbers[SIZE]{};
    int count{};


    // Get file name from the user
    cout << "Enter the file name: ";
    cin >> filename;

    // Open the file
    inputFile.open(filename);

    // If the file opened successfully, process it
    if (!inputFile)
    {
    // display an error message if the file was not found
    cout << "Error opening the file.\n";

   return 1;
    }

    // read the #'s from the file & display them.
    while (count < SIZE && inputFile >> numbers[count])
    {
        count++;
    }

    // close the file
    inputFile.close();


    // sort the array using selection sort
 void selectionSort(int array[], int size){

       int startScan, minIndex, minValue;

       for (startScan = 0; startScan < (size - 1); startScan++)
       {
            minIndex = startScan;
            minValue = array[startScan];
            for(int index = startScan + 1; index < size; index++)
            {
                if (array[index] < minValue)
                {
                    minValue = array[index];
                    minIndex = index;
                }
            }
            array[minIndex] = array[startScan];
            array[startScan] = minValue;
       }
}

    // prompt the user to enter a # to search the array or Q to quit




    // perform a binary search to locate the number, If found display
    int binarySearch(int array[], int size, int value) <-- here
 {

        int first = 0, // First array element
        last = size - 1, // Last array element
        middle, // Mid point of search
        position = -1; // Position of search value
        bool found = false; // Flag

        while (!found && first <= last)
        {
              middle = (first + last) / 2; // Calculate mid point
              if (array[middle] == value) // If value is found at mid
              {
                  found = true;
                  position = middle;
              }
              else if (array[middle] > value) // If value is in lower half
                 last = middle - 1;
              else
                 first = middle + 1; // If value is in upper half
        }
return position;
 }
}


    // "Number %d Found at Position %d". Where the
    //first %d is the number the user is searching for
    // and the second %d is the location in the SORTED ARRAY.
    //Else display "Number %d Not Found", where you will put the number the user is searching for in place of %d. You can use either printf() or cout.


    // search continues until they hit Q since it's a loop.
Last edited on
You can't define one function inside another function. All your functions appear to be inside main().

If you used a consistent indent style you would probably be able to see the problem much easier:

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

constexpr int SIZE{ 200 };

int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);



int main()
{

    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    int numbers[SIZE] {};
    int count{};
    
    
    // Get file name from the user
    cout << "Enter the file name: ";
    cin >> filename;
    
    // Open the file
    inputFile.open(filename);
    
    // If the file opened successfully, process it
    if(!inputFile)
    {
        // display an error message if the file was not found
        cout << "Error opening the file.\n";
        
        return 1;
    }
    
    // read the #'s from the file & display them.
    while(count < SIZE && inputFile >> numbers[count])
    {
        count++;
    }
    
    // close the file
    inputFile.close();
    
    
    // sort the array using selection sort
    void selectionSort(int array[], int size)
    {
    
        int startScan, minIndex, minValue;
        
        for(startScan = 0; startScan < (size - 1); startScan++)
        {
            minIndex = startScan;
            minValue = array[startScan];
            for(int index = startScan + 1; index < size; index++)
            {
                if(array[index] < minValue)
                {
                    minValue = array[index];
                    minIndex = index;
                }
            }
            array[minIndex] = array[startScan];
            array[startScan] = minValue;
        }
    }
    
    // prompt the user to enter a # to search the array or Q to quit
    
    
    
    
    // perform a binary search to locate the number, If found display
    int binarySearch(int array[], int size, int value) < -- here
    {
    
        int first = 0, // First array element
        last = size - 1, // Last array element
        middle, // Mid point of search
        position = -1; // Position of search value
        bool found = false; // Flag
        
        while(!found && first <= last)
        {
            middle = (first + last) / 2; // Calculate mid point
            if(array[middle] == value)  // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if(array[middle] > value)  // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1; // If value is in upper half
        }
        return position;
    }
}


// "Number %d Found at Position %d". Where the
//first %d is the number the user is searching for
// and the second %d is the location in the SORTED ARRAY.
//Else display "Number %d Not Found", where you will put the number the user is searching for in place of %d. You can use either printf() or cout.


// search continues until they hit Q since it's a loop. 


I tried indenting more neatly like your example and i have the same exact error, i don't understand.
What don't you understand? You're trying to define multiple functions inside of main() which is not allowed in C++. Perhaps you have the closing brace for main() in the wrong place?

i have the same exact error,

Move the closing brace } at line 102 in your code to line 49. Does it compile now?
FanOfThe49ers wrote:
I tried indenting more neatly
jlb did not say that indenting fixes anything. (It does not; C++ is not like Python or YAML, where indentation has syntactic meaning.)
jlb wrote:
you would probably be able to see the problem


The problem is:
compiler wrote:
function-definition is not allowed here

Which jlb translated to:
You can't define one function inside another function.


Function's definition (aka implementation) has:
1
2
3
4
return-type identifier ( params )
{
  statements
}

It is not legal (except the lambda closures) to have definition of one function among the statements of another function:
1
2
3
4
void foo() {
  void bar() { // ERROR: function-definition is not allowed here
  }
}

Without the indentation it is simply harder to see where one function ends:
1
2
3
4
void foo() {
void bar() { // ERROR: function-definition is not allowed here
}
}


You have to define each function separately:
1
2
3
4
5
void foo() {
}

void bar() {
}

Okay well i defined them within their own braces... and now i have 14 errors saying a bunch of things haven't been defined & missing template arguments?

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <string>
#include <array>

using namespace std;

constexpr int SIZE{ 200 };

int binarySearch(int array[], int size, int value);
void selectionSort(int array[], int size);



int main()
{

    // prompt user to enter file name. also display if it exists or not
    ifstream inputFile; // gives access to file
    string name, filename;
    int numbers[SIZE] {};
    int count{};


    // Get file name from the user
    cout << "Enter the file name: ";
    cin >> filename;

    // Open the file
     inputFile.open(filename.c_str());

    // If the file opened successfully, process it
    if(!inputFile)
    {
        // display an error message if the file was not found
        cout << "Error opening the file.\n";

        return 1;
    }

    // read the #'s from the file & display them.
    while(count < SIZE && inputFile >> numbers[count])
    {
        count++;
    }

    // close the file
    inputFile.close();

    // sort the array using selection sort
    {
    void selectionSort(int array[], int size);
    }

        int startScan, minIndex, minValue;

        for(startScan = 0; startScan < (size - 1); startScan++)
        {
            minIndex = startScan;
            minValue = array[startScan];
            for(int index = startScan + 1; index < size; index++)
            {
                if(array[index] < minValue)
                {
                    minValue = array[index];
                    minIndex = index;
                }
            }
            array[minIndex] = array[startScan];
            array[startScan] = minValue;
        }


    // prompt the user to enter a # to search the array or Q to quit


    // perform a binary search to locate the number, If found display
    {
    int binarySearch(int array[], int size, int value); //< -- here
    }

        int first = 0, // First array element
        last = size - 1, // Last array element
        middle, // Mid point of search
        position = -1; // Position of search value
        bool found = false; // Flag

        while(!found && first <= last)
        {
            middle = (first + last) / 2; // Calculate mid point
            if(array[middle] == value)  // If value is found at mid
            {
                found = true;
                position = middle;
            }
            else if(array[middle] > value)  // If value is in lower half
                last = middle - 1;
            else
                first = middle + 1; // If value is in upper half
        }
        return position;



}
// "Number %d Found at Position %d". Where the
//first %d is the number the user is searching for
// and the second %d is the location in the SORTED ARRAY.
//Else display "Number %d Not Found", where you will put the number the user is searching for in place of %d. You can use either printf() or cout.


// search continues until they hit Q since it's a loop.
Okay well i defined them within their own braces
Nobody said you should define the functions within their own braces.

The point is that you cannot define a function within a function. In your original code, you tried to define selectionSort() and binarySearch() inside the main() function. C++ doesn't allow that.

All you have to do is move those two function (including their code), outside the main() function. So in jlb's version,put lines 50-102 after line 103. I think you'll find that it compiles after that change.
1
2
3
4
5
6
7
8
9
 // incorrect
int main()
{
    int my_arr[3] = {3, 1, 2};
    void sort(int arr[], int size)
    {
        // ...
    }
}


1
2
3
4
5
6
7
8
9
10
11
 //correct:
void sort(int arr[], int size)
{
    // ...
}

int main()
{
    int my_arr[3] = {3, 1, 2};
    sort(my_arr, 3);
}
Last edited on
@FanOfThe49ers do you realize that every starting brace must have a matching ending brace? In your latest code you added several beginning braces without any ending braces.

The problem is that the ending brace for main() is in the wrong place, causing your functions to be defined inside of main.
Lets repeat:
You can't define one function inside another function.

You must write each function separately:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

constexpr int SIZE{ 200 };

int binarySearch(int array[], int size, int value); // declaration
void selectionSort(int array[], int size); //declaration

int main() {
  // code of main()
} // main() ends here


// sort the array using selection sort
void selectionSort(int array[], int size) {
  // code of selectionSort()
} // selectionSort() ends here


// perform a binary search to locate the number
int binarySearch(int array[], int size, int value) {
  // code of binarySearch()
} // binarySearch() ends here 

ohhh now i get it, the emphasis of the bolded brackets made it very clear. I do indeed have no errors now, i'm still pretty new to programming so seeing things a couple different ways helps a ton, thanks guys. So the overall concept is any additions/functions for my main function must be done in separate brackets, outside of main. ( {} ). Is there an overall concept as to what your main code is, for example since I start the program asking for the file from user, anything else i add automatically goes into new functions outside of main, if that makes sense? I'm just trying to make sure I understand this correctly. Another random example such as: say i'm doing a calculator asking for numbers from the user, that'd be in main & the next step to add the operators which would be done in new braces outside of main.
There's no single answer for everything; good code organization is what beginners and 'experts' alike can struggle with. At the very least, if you notice yourself having duplicate code in multiple places, that's an indication that you should factor that code out into a function.

A function does something, and its name should describe what's happening. For your calculator example: Getting input from the user could be its own function. Calculating the final calculator result given the input from the user (passed in as a parameter) could be another function. You break your code into logical chunks in a way to make what's happening as clear as possible.
Okay got it, the better you become the better it should be organized and easier to read, thanks for the feedback i appreciate it.
Topic archived. No new replies allowed.