Hey, we just started learning functions in class. We were given a project to complete. I'm trying to work out the errors, but I'm stuck with
error C2084: function 'void bubblesort(int [],int)' already has a body
error C2084: function 'void print(int [],int)' already has a body
error C2084: function 'void printH(int [],int)' already has a body

How do I call the functions differently to get them to not conflict?

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

void bubblesort(int [], int );
void bubblesort(int *, int ); // function overloading to sort characters
void print(int[], int );
void printH(int[], int );
void print(int *, int );  // function overloading to print characters
void printH(int *, int );   // function overloading to print histogram for characters

int main()
{
int i, magic, size;
int high;
int low;
int a[100]; // declare an integer array named a with size 100
int b[100]; // declare a character array named b with size 100

cout << "how many random numbers do you want to create? (no more than 100)\n";
cin >> size; // read user input into variable size
if (size > 100) 
{
    cout << "too many, program ends.\n";
    return -1;
}
cout <<"enter the smallest and largest random number you want to create:\n";
cin >> low >> high; // read user input into variables low and high

srand(time(NULL));
for ( int i = 0; i < size; i++) //write a for loop to run the code inside { }. The total loops = size. 
{
    magic = rand() % (high-low+1) + low;
    a[i] = magic;
    srand(time(NULL)+i*i+3);
}

cout << "\nthe random numbers generated are: \n";
cout << a << endl; //write a statement to call print function to print a
bubblesort (a, size);
cout << "\nthe random numbers after sort:\n";
cout << a << endl; //write a statement to call print function to print a
cout << "\nthe histogram of the numbers:\n";
printH(a, size);

cout << "\n\nhow many random lower case characters do you want to create? (no more than 100)\n";
cin >> size;
if (size > 100) 
{
    cout << "too many, program ends.\n";
    return -1;
}

high = 122;
low = 97;
srand(time(NULL));
for (i = 0; i < size; i++)
{
    magic = rand() % (high-low+1) + low;
    b[i] = magic;
    srand(time(NULL)+i*i+3);
}

cout << "\nthe characters generated are: \n";
print(b, size);
bubblesort (b, size);
cout << "\nthe characters after sort are: \n";
print(b, size);
cout << "\nthe histogram of the charaters:\n";
printH(b,size);
return 0;
}

void bubblesort( int x[], int len )
{
//write function definition to bubble sort the integer array x[], len is the size of x[]
  for(int i=i; i<len; i++){ 
    for(int j=len-1; j>=i; j--) { 
      if(x[j-1] > x[j]) {
         j = x[j-1]; 
         x[j-1] = x[j]; 
         x[j] = j; 
      } 
    } 
  }
}
void bubblesort( int x, int size )
{
//write function definition to bubble sort a character array
for(int i = 0; i < size; i++){
    for(int j = i + 1; j < size - 1; j++){
        if(x[j-1] > x[j]){
            char temp = x[j];
            x[j] = x[j-1];
            x[j-1] = temp;
        }
    }  
}
}
void print (int a[], int len)
{
//write function definition to print an integer array
    for(int i=0; i<len; i++){ 
        cout << a[i] << " ";
  }

}
void printH (int a[], int len)
{
//function definition to print histogram of a sorted integer array
int current, shows;
current = -1;
shows = 0;
for (int i = 0; i < len; i++)
{
    if (current != a[i] )
    {
        current = a[i];
        cout << "\n" << a[i] << " *";
    }
    else
        cout << "*";
}
cout << endl;
}
void print (int *a, int len)
{
//write function definition to print a character array
for(int i=0; i<len; i++){ 
        cout << a[i] << " ";
  }
}
void printH (int *a, int len)
{
//write function definition to print histogram of a sorted character array
char temp;
for(int i = 0; i < len; i++){
    for(int j = i + 1; j < len - 1; j++){
        if(a[j-1] > a[j]){
            temp = a[j];
            a[j] = a[j-1];
            a[j-1] = temp;
        }
    }  
}
}
rpcob
Deleted Member

Okay, the issue is that void f(int *a, int b) is literally identical to void f(int a[], int b) from the compilers perspective, as such there is a conflict. This is because arrays are just stored as pointers. You can either remove the duplicate functions or rename them to fix the problem.

Quick solution is to swap the parameters of one of the functions. Could be a little weird, but it helps I guess.

Function names are like variables you can't over lap them bubblesort was used twice as Labdabeta pointed either take one of them out (which is not an option for you since one is for int and the other is for chars) just rename one of them

Captain119: Actually, in C++ you can overload the names of functions; the issue is that you have to give each of the different functions sharing their name a different signature. The real issue is that the OP made a mistake in the second function's declaration; it should be something like this:

void bubblesort( char ch[], int size); 

This passes a character array to the function, which is what it is supposed to be sorting.

Except that the char type is only for standard ASCII characters, sometimes one must deal with Unicode characters, which (if I remember correctly) are stored in ints. As such he only has a few options aside from renaming the second functions. Off the top of my head he could use a charMode() or intMode() setup of some kind. (with either boolean parameters or function pointers, or something)

Actually, the correct type for 'wide' characters is wchar_t, as of the 1998 C++ standard. The C++ 11 standard expands on this, but it still has wchar_t for most Unicode and other non-AsSCII encodings.

Oh oops.....that would be really confusing but it does make sense in a way thanks for explaining that Schols

Labdabeta i did manage to get his program to work he had a few other mistakes in there

and i just realized the person got deleted...makes my response ever dumber lol

did you get it to work? can you send me the working code? i am interested in this.

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

void bubblesort(int [], int );
void bubblesortChar(int *, int ); // function overloading to sort characters
void print(int[], int );
void printH(int[], int );
void printChar(int *, int );  // function overloading to print characters
void printHChar(int *, int );   // function overloading to print histogram for characters

int main()
{
    int i, magic, size;
    int high; 
    int low;
    int a[100]; // declare an integer array named a with size 100
    int b[100]; // declare a character array named b with size 100
    int *AP = a;

    cout << "how many random numbers do you want to create? (no more than 100)\n";
    cin >> size; // read user input into variable size
    if (size > 100)
    {
        cout << "too many, program ends.\n";
        return -1;
    }

    cout <<"enter the smallest and largest random number you want to create:\n";
    cin >> low >> high; // read user input into variables low and high

    srand(time(NULL));
    for ( int i = 0; i < size; i++) //write a for loop to run the code inside { }. The total loops = size.
    {
        magic = rand() % (high-low+1) + low;
        a[i] = magic;
        srand(time(NULL)+i*i+3);
    }

    cout << "\nthe random numbers generated are: \n";
    //cout << a << endl; //write a statement to call print function to print a
    print (a, size);

    cout << "\nthe random numbers after sort:\n";
    //cout << a << endl; //write a statement to call print function to print a
    bubblesort(a, size);

    cout << "\nthe histogram of the numbers:\n";
    printH(a, size);
    cout << "\n\nhow many random lower case characters do you want to create? (no more than 100)\n";
    cin >> size;
    if (size > 100)
    {
        cout << "too many, program ends.\n";
        return -1;
    }

    high = 122;
    low = 97;
    srand(time(NULL));
    for (i = 0; i < size; i++)
    {
        magic = rand() % (high-low+1) + low;
        b[i] = magic;
        srand(time(NULL)+i*i+3);
    }

    cout << "\nthe characters generated are: \n";
    printChar(b, size);
    cout << "\nthe characters after sort are: \n";
    bubblesortChar (b, size);
    cout << "\nthe histogram of the charaters:\n";
    printHChar(b,size);
    return 0;
}

void bubblesort( int x[], int len )
{
    //write function definition to bubble sort the integer array x[], 
    //len is the size of x[]
    for(int i=0; i<len; i++)
    {
        int Smallest = i;
        for(int j= i + 1; j < len; j++) 
        {
            if(x[j] < x[Smallest]) 
                Smallest = j;
        }
        swap(x[i], x[Smallest]);
        cout << x[i] << " ";
    }
}

void bubblesortChar( int x[], int size )
{
    for(int i=0; i < size; i++)
    {
        char Smallest = i;
        for(int j= i + 1; j < size; j++) 
        {
            if(x[j] < x[Smallest]) 
                Smallest = j;
        }
        swap(x[i], x[Smallest]);
        cout << x[i] << " ";
    }
}

void print (int a[], int len)
{
    //write function definition to print an integer array
    for(int i=0; i<len; i++)
    {
        cout << a[i] << " ";
    }
}

void printH (int a[], int len)
{
    //function definition to print histogram of a sorted integer array
    int current, shows;
    current = -1;
    shows = 0;
    for (int i = 0; i < len; i++)
    {
        if (current != a[i] )
        {
            current = a[i];
            cout << "\n" << a[i] << " *";
        }
        else
            cout << "*";
    }
    cout << endl;
}

void printChar (int *a, int len)
{
    //write function definition to print a character array
    for(int i=0; i<len; i++)
    {
        cout << a[i] << " ";
    }
}

void printHChar(int *a, int len)
{
    int current, shows;
    char temp;
    current = -1;
    shows = 0;
    for (int i = 0; i < len; i++)
    {
        if (current != a[i] )
        {
            temp = a[i];
            cout << "\n" << a[i] << " *";
        }
        else
            cout << "*";
    }
    cout << endl;

}
This article has been dead for over six months. Start a new discussion instead.