variable or field "MergeSort" declared void?

I am having trouble with this compiler error. If someone could help me fix it I would be grateful.

main.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
#include "MergeSort.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int compare(const ItemType&, const ItemType&);

int main()
{
    const unsigned MAX = 10;
    ItemType a[MAX];
    
    for (unsigned i = 0; i < MAX; i++)
    {
        a[i] = rand()%2000;
    }
    MergeSort(a, MAX, compare);
    ItemType first = a[0];
        
    for (unsigned i = 1; i < MAX; i++)
    {
        ItemType next = a[i];
        if (next < first)
        {
            cout << "Error" << a[0] << " > " << a[i] << endl;
            first = next;
        }
    }
        
    return 0;
};

int compare (const ItemType& i1, const ItemType& i2)
{
    return int(i1-i2);
}
            


MergeSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void MergeSort(ItemType* base, size_t nelem, 
                int(*fcmp)(const ItemType&, const ItemType&))
{
    if (nelem > 1)
    {
        size_t nelem1 = nelem/2;
        ItemType* base2 = base + nelem1;
        MergeSort(base, nelem1, fcmp);
        mergeSort(base2, nelem - nelem1, fcmp);
        merge(base, nelem, fcmp);
    }
}

void merge(ItemType* base, size_t nelem, 
            int(*fcmp)(const ItemType&, const ItemType&))
{
}


MergeSort.h
1
2
3
4
5
6
7
#include <cstdlib>

typedef float ItemType;

void MergeSort(ItemType*, size_t, int(*fcmp)(const ItemType&, const ItemType&));
            
            


Gives the following compiler error:


1. variable or field "MergeSort" declared void
1. 'ItemType' was not declared in this scope
1. 'base' was not declared in this scope
....
When the compiler tries to compile the file named MergeSort.cpp, it has no idea whan an ItemType is. #include the header file that defines an ItemType.
Last edited on
Its always something simple like that :p. Thank you. Marked as solved.
It looks like you forgot include the header in the cpp file.
Topic archived. No new replies allowed.