Need help figuring out error

Hey guys, I was wondering if you guys could take a look at my code. I am trying to simply implement Bubble Sort, but I am having trouble getting it to compile. I managed to get it to compile and run successfully just using functions in my main file, but when I put my functions into a class and try and run it from a class file. I keep getting these errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/nawgee/NetBeansProjects/BubbleSort'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/bubblesort
make[2]: Entering directory `/home/nawgee/NetBeansProjects/BubbleSort'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/bubblesort build/Debug/GNU-Linux-x86/main.o 
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/nawgee/NetBeansProjects/BubbleSort/main.cpp:23: undefined reference to `BubbleSort::RandomArray(int*)'
/home/nawgee/NetBeansProjects/BubbleSort/main.cpp:25: undefined reference to `BubbleSort::PrintSort(int*, int)'
/home/nawgee/NetBeansProjects/BubbleSort/main.cpp:27: undefined reference to `BubbleSort::B_Sort(int*, int)'
/home/nawgee/NetBeansProjects/BubbleSort/main.cpp:29: undefined reference to `BubbleSort::PrintSort(int*, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/bubblesort] Error 1
make[2]: Leaving directory `/home/nawgee/NetBeansProjects/BubbleSort'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/nawgee/NetBeansProjects/BubbleSort'
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s) 


Here's my code:

MAIN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "BubbleSort.h"

using namespace std;

const int MAX = 10;    //the maximum number of elements that the array can hold

int main()
{
    int b[MAX];
    BubbleSort sort;
    
    sort.RandomArray(b);

    sort.PrintSort(b, MAX);

    sort.B_Sort(b, MAX);

    sort.PrintSort(b, MAX);

    return 0;

}


BUBBLESORT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BUBBLESORT_H
#define	BUBBLESORT_H

#include <cstdlib>

using namespace std;

class BubbleSort{
public:
        void RandomArray(int a[]);      //This function will generate random integers in array
        void B_Sort(int a[], int length);   //This function will implement BubbleSort Algo
        void PrintSort(int a[], int length);    //This function will print out Array
};//End BubbleSort Class

#endif	/* BUBBLESORT_H */ 


BUBBLESORT.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
39
#include "BubbleSort.h"

void BubbleSort::RandomArray(int a[])
{
    int i;

    srand(time(0));        //seeds, so that a new algorithm can be used for rand()

    for(i = 0; i < MAX; i++)
        a[i] = rand() % 100;        //this generates a random number from 0 - 100
}//End RandomArray

void BubbleSort::B_Sort(int a[], int length)
{
   int i, j, temp;

   for(i = 0; i < length; i++) 
   {
       for(j = 0; j < i; j++)
       {
           if(a[i] > a[j])
           {
               temp = a[i];  
               a[i] = a[j];             //This switches elements in Bubble Sort
               a[j] = temp;
           }
       }
   }
}//End B_Sort

void BubbleSort::PrintSort(int a[], int length)
{
   int i;

   for(i = 0; i < length; i++)
       cout << a[i] << ", ";

   cout << endl << endl;
}//End PrintSort 


No this is not homework, I'm trying to get a feel for different data structures so that I can start building myself a professional portfolio. Thanks in advance
You didn't include BUBBLESORT.cpp in your project.

And you don't need <cstdlib> in BUBBLESORT.h. Put it in BUBBLESORT.cpp instead.
And never, NEVER put using namespace std; in headers. This will make everyone using you library want to brutally murded you.
Last edited on
LOL thanks for the speedy response MiiNiPaa!

I added <cstdlib> and the using namespace std; to the .cpp file instead.

Everything is working the way it should now, I thought by including the "BubbleSort.h" file that this would suffice for main?
#include directive executed by preprocessor and basically means "copy-paste all content of <> file here" As you can see there is no way to tell which file should be included to your project. Actually you don't need to name you cpp file accordingly to .h file. You can have 2 cpp files for one .h file or one cpp for two .h.

More: you can compile main.cpp and bubblesort.cpp on two different computers and link them on third, but it is advanced topic :)
Topic archived. No new replies allowed.