Search code examples
c++disjoint-setsdisjoint-union

My C++ function gives unusual error, regarding declaration


#include <bits/stdc++.h>
using namespace std;

void union(int x, int y, int link[], int size[])
{
    int a = find(x, link); int b = find(y, link);
    if (size[a] < size[b])
        swap(a,b);
    if ( a != b)
    {
        size[a] += size[b];
        link[b] = link[a];
    }
}


int main()
{
    cout<<"Hello World";

    return 0;
}

I just can't figure out what the hell I am doing wrong. I checked multiple times but to no avail. It's probably something very stupid. It keeps giving the following errors.

file.cpp:19:11: error: expected identifier before '(' token
void union(int x, int y, int link[], int size[])
        ^
file.cpp:19:12: error: expected unqualified-id before 'int'
void union(int x, int y, int link[], int size[])
            ^~~
file.cpp:19:12: error: expected ')'
before 'int'

Solution

  • union is a reserved keyword in C++, and so, you cant use it for a function name. Rename it to something else, for example union_. That will solve your problem.