Trying to pass char* array in function, but stuck

Hi everyone. I'm trying to, currently, learn how to effectively use .h and .cpp files separately. I'm using a bubblesort program I wrote to get me started but I'm stuck!

I get this error in Xcode now, after chopping up my code:
error: no matching function for call to 'mySort::bubbleSort(const char* [2000])'

It sounds simple enough, my functions don't match. My program also used to work with all the code in one file. I would greatly appreciate any help on this as I'm not very fluent in C++ yet.

Thanks, Ivan

names.h
(I have 2000 I'm sorting but I cut it.)
1
2
3
4
5
6
7
8
9
#define NUM_NAMES 2000

const char* names[NUM_NAMES] = 
{
	"Jacob",
	"Michael",
	"Joshua",
	"Matthew",
};



mySort.h
1
2
3
4
5
6
7
8
9
10
#include "names.h"

class mySort
{
public:
	void bubbleSort(const char *names[], const int length);
	void print(const char *names[], int length);
	
};



mySort.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
#include <iostream>
#include <strings.h>
#include "mySort.h"

void mySort::bubbleSort(const char *data[], const int length = NUM_NAMES)
{
	int i, j, place;
	
	for(i = 0; i < length; i++)
	{
		for(j = 0; j < i; j++)
		{
			place = strcasecmp(data[i], data[j]);
			
			if(place < 0)
			{
				const char* temp = data[i];
				data[i] = data[j];
				data[j] = temp;
			}
		}
	}
}

void mySort::print(const char *data[], int length)
{
	int i;
	
	for(i = 0; i < length; i++)
		std::cout << i + 1 << ". " << data[i] << std::endl;
}




mySort_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "mySort.h"

int main()
{
	mySort bs;
	
	bs.bubbleSort(names);
	
	bs.print(names, 50);
	
	return 0;
}
Last edited on
Try putting the default parameter initialization for mySort::bubbleSort() in the header.
I had to delete my previous faux-success post. I compiled another project thinking it was the one we're talking about.

I tried the suggestion to put this in mySort.h :
void bubbleSort(const char *names[], const int length = 2000);

I get this new strange error in Xcode:
Command /Developer/usr/bin/g++-4.0 failed with exit code 1
or when I copy and paste the error in here:
collect2: ld returned 1 exit status

Update: Searched around the net and apparently this is a linker error and not a compiler one. My first program with many surprises :)
Last edited on
If I remember correctly (I'm not in front of my Mac at the moment), in XCode you must make sure that your .cpp files are listed under the "Compile Sources" phase of your "Target".

If they're not you need to add them (just dragging them should suffice). If not, they won't be compiled so the linker won't find the .o file. There should also be a small radio button in the top pane of the editor window next to the filename which should do the same thing.

That has caught me out a couple of times.
Topic archived. No new replies allowed.