0

Using CodeLite IDE on a Mac to brush up my C++. It's a common practice to write functions in a different cpp file than the one that has the main(). I have two .cpp files, main.cpp and InsertionSort.cpp. The latter has the sorting function and the printing function.

The main is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv) 
{
    int unsorted[6] = {5,2,4,6,1,3};
    int size = 6;
    insertionSort(unsorted, size);
    printArray(unsorted, size);     
}

and the other insertionSort.cpp is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

void printArray(int*,int);

void insertionSort ( int array[], int size)
{

int value = 0; //temp variable to store the value of the element being moved
int holeIndex = 0; // index variable to keep track of the vacant slot
for (int i = 1; i <= size ; i++)
{
    value  = array[i];
    holeIndex = i;
    while(holeIndex > 0 && value < array[holeIndex-1])
    {
        array[holeIndex] = array[holeIndex -1];
        holeIndex = holeIndex -1 ;
    }
    array[holeIndex] = value;
    printArray(array,size);
}
//return array;
}

void printArray(int array[], int size)
{
for (int i = 0; i<size; i++ )
{
    cout << array[i]<<" ";
}
    cout << endl;
}

the problem i'm facing is that i get an undeclared identifier 'insertionSort' and undeclared identifier 'printArray' while i run this. It seems to be an import issue, but i can't figure it out in CodeLite. Also, any general rule of thumb to follow to import the other source .cpp files so I don't run into this noob problem again ? Any help would be greatly appreciated. Thanks in advance.

any include i'm missing?

baconSoda
  • 503
  • 2
  • 5
  • 15
  • none of the answers in the linked duplicate question have helped. =/ It works if i keep all of it in one file, but not if ive split them into the 2 files like i mentioned. – baconSoda Aug 05 '16 at 06:25
  • Hint: the "missing header" one. – juanchopanza Aug 05 '16 at 06:30
  • #include But i run into another error which says duplicate symbols for architecture x86_64 . The problem is that if i don't include the iostream or the namespace std then i have to implicitly type std::cout. thanks for the hint though. I like trying to figure it out , but this is getting on my nerves. seems like i'm overlooking something simple – baconSoda Aug 05 '16 at 06:36
  • No, make a header with the function declarations. This is all basic, trivial stuff that should be explained in beginners tutorials. Not really a question for SO. – juanchopanza Aug 05 '16 at 06:40
  • Alright. Thanks @juanchopanza. I guess i have to go through the tutorials more thoroughly. will update if i still face a problem. Appreciate the help ! – baconSoda Aug 05 '16 at 06:50

0 Answers0