New to arrays...

Hi all, I'm taking an intro to c++ class and our assignment is to type up this example (https://books.google.com/books?id=et8hDgAAQBAJ&pg=PA610&lpg=PA610&dq=chapter+8+example+8+ds+malik+arrays+help&source=bl&ots=4TnvETXE8l&sig=qVAg7LUs-dre69ps_cqEHJgDkDg&hl=en&sa=X&ved=0ahUKEwiH0drYo_DTAhVCImMKHc_zB28Q6AEIRzAF#v=onepage&q&f=false page 546) and get it to run. I've typed it up, checking it several times, but I keep getting an error that says "undefined reference to 'seqSearch(int const*, int, int)'. I've read through the chapter and done some research online but I'm still unsure of how to solve this... Thanks for any help you can give!

//preprocessor directives/header functions
#include <iostream>

//standard library
using namespace std;

const int ARRAY_SIZE = 10;

int seqSearch(const int list[], int listLength, int searchItem);

//enter function main
int main()
{
int intList[ARRAY_SIZE];
int number;

cout << "Enter" << ARRAY_SIZE << " integers." << endl;

//reads data into intList array
for (int index = 0; index < ARRAY_SIZE; index++)
cin >> intList[index];

cout << endl;

cout << "Enter the number to be searched";
cin >> number;
cout << endl;

int pos = seqSearch(intList, ARRAY_SIZE, number);

if (pos != -1)
cout << number << "is found at index " << pos << endl;

else
cout << number << "is not found at index" << endl;

return 0;
}
undefined reference to 'seqSearch(int const*, int, int)

You have the prototype of this function...

int seqSearch(const int list[], int listLength, int searchItem);

but not the actual definition of it, at least in the code posted here.
Okay, how do I write a definition for it? Does that mean I need to declare what each element of the array is? Thanks for your help, I missed one class and I've fallen behind.

Extra: One of my peers got the example to run without making any alterations.I'm trying to follow along with https://codescracker.com/cpp/cpp-function-definition.html but the example mine is based on is a little different.
int seqSearch(const int list[], int listLength, int searchItem);
This line tells the compiler that there is a function named seqSearch that accepts three parameters and returns an int. From the name, it probably performs a sequential search.

That line does not tell the compiler how to actually perform a sequential search - i.e., you need to define sequential search for the compiler.

Such a definition looks like this:
1
2
3
int seqSearch(const int list[], int const listLength, int const searchItem) { 
  // your code goes here
}


You'll need to loop through each element of the array until you find searchItem, and then return its position.
Last edited on
Check in the book you posted the link from - there seems to be a definition of that function on page 545, at least in the preview I get. (Not all the pages are available)
Ah! I've got it. You're right wildblue, I needed that chunk of code from above. I have a better understanding of arrays now. Thanks to both of you!
Topic archived. No new replies allowed.