I am trying to declare a function to find the largest mobile element for a program to list permutations using johnson-totter algorithm. Here is my definition of the function in the header file:

int findMobile( string, int[] );

and here is my actual declaration of the function:
int findMobile( string word, int[] dir ){

and the error I am getting is

error: expected ',' or '...' before 'dir', error: 'dir' was not declared in this scope

I was looking at an article that had function declartions with arrays as reference and they did something exactly as this but mine isn't working. Am I missing something obvious? This is my first class using c++ but it is not about learning the language it is about data structures and algorithms so it is assumed we have backgrounds in other languages and we can just pick this one up quickly. What is the problem with this function? The call looks like:

index = findMobile( word, dir );

and dir is declared by the following line:
int dir [index] but that shouldn't even matter as passing the array to the function doesn't actually care what variable I am passing it is just looking for an argument that matches the type and that is where my problem is, it isn't allowing me to make a function with an int array as an argument. I tried int* using pointers but it has been a long time since I've used a pointer and that just resulted in a segfault because I wasn't sending references or using the pointer right or anything. Can anyone help, I will provide any answers to questions that I can. I don't want to put the entire code out there just because we are supposed to do this on our own and I think the code will work but I can't test it because it won't compile. If it doesn't work then I may need to ask some questions.

try int dir[] in the function definition (the declaration is in the header - the definition is in the source file by standard convention). Another thing, you will be making a copy of the array this way. If the array is big, It is better to use a pointer to the array as in:

// Declaration
int findMobile( const string&, int* ) const;

// Definition
int findMobile( const string& word, int* dir ) const { return dir[0]; }

An alternative is to use std::array<int> or std::vector<int> instead for the array, and pass that variable by reference. Since you are using C++, that would be a better solution.

Also, if you are not modifying the array, I would suggest that you use a const qualifier for that argument like I did for word.

Final point. If you do use a pointer to the array, you absolutely should test it for null in the function before you access it, as in: if (0 == dir) { /* fail */ return -1; }

One last thing. If in your function you are iterating through the array, put a flag in the last position, such as -1 for int arrays, so when you hit that value, you know you have reached the end of the array. The std::array and std::vector classes have a size() method that allows you to iterate to the end quite safely, otherwise you either need a placeholder / flag for the end of the array, or pass the number of elements in the array as an argument to the function.

FYI, std::array<T,size_t N> is only for C++11. For earlier versions of C++, use std::vector<T>. The std::array<T,size_t> class is presized using the second argument in the template declaration. Myself, I prefer vectors. For small collections, it is about as efficient as arrays, and the resize algorithm is quite efficient. Before the STL was invented I wrote a container class that used the same algorithm they use for std::vector<T>. You could insert a 100,000 records in practically no time.

I use std::vector<T> objects frequently for the work I am doing now for Panasonic Factory Solutions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.