header file issues

cpp file not reading the header file.. I think

Page 1 of 1

8 Replies - 1583 Views - Last Post: 18 September 2010 - 05:20 PM Rate Topic: -----

#1 D_MeNtEd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-August 10

header file issues

Posted 17 September 2010 - 05:06 PM

Okay, So I'm doing a programming class and I get most of it, but the class part of it is being a constant pain for me. The program is to change a non recursive version of a sequential search algorithm, and change it to a recursive version... The example was taken from the book (non recursive) and I can't get that one to work so I can try to figure out how to change it over to recursive.

The header file is as follows:
template <class elemType>
int seqSearch (const elemType list[], int length, const elemType& item)

{
    int loc;
    bool found = false;
    
    for (loc = 0; loc < length; loc++)
    {
        if (list[loc] == searchItem)
        {
         found = true;
         break;
         }
}
if (found)
return loc;
else
return -1;
}



then the cpp is:
#include <iostream>
#include "seqsearch.h"

using namespace std;

int main ()
{
    int intList[] = {2, 16, 34, 45, 53, 56, 70, 75, 96};
    
    int pos;
    pos = binarySearch (intList, 10, 45);
    
    if (pos != -1)
    cout << " Line 5: " << 45 << " found at position " << pos << endl;
    
    else
    cout << " Line 7: " << 45 << " is not in the intList " << endl;
    
    system("Pause");
    return 0;
}



when I try to compile it I get the error "infile included from seqsearch.cpp"

As I said, I can't wrap my head around this class stuff as is... so I'm not sure what to do with this. I do have both files saved in the same folder..

Suggestions? Help? Please!

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 17 September 2010 - 06:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: header file issues

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: header file issues

Posted 17 September 2010 - 05:36 PM

I'm guessing your professer makes you use
system("pause");
but if he dosn't you should use
cin.get();
instead and sometimes that won't work if there is already input in the buffer then you use
 cin.ignore(); 
cin.get();


Hope i was helpful. If i was please click the green plus button in the bottom right hand corner.

This post has been edited by Alex6788: 17 September 2010 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: header file issues

Posted 17 September 2010 - 05:47 PM

your header file should be a .h file and not a .cpp file...


According to your error it seems as though you saved your header file as a .cpp instead. Maybe check the file extension?

Otherwise, IDK what your issue is.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: header file issues

Posted 17 September 2010 - 06:32 PM

You didn't post the WHOLE error, but I'll make an educated guess.

template <class elemType>
int seqSearch (const elemType list[], int length, const elemType& item)

{
    int loc;
    bool found = false;
    
    for (loc = 0; loc < length; loc++)
    {
        if (list[loc] == searchItem)



searchItem and item aren't the same thing, are they?
Was This Post Helpful? 0
  • +
  • -

#5 Guest_D_MeNtEd*


Reputation:

Re: header file issues

Posted 18 September 2010 - 04:55 PM

Sorry about not posting using the code tags, I wasn't sure what they were.

It's not a cin.get() buffer issue.

The full error message is

2 C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\seqsearch.cpp In file included from seqsearch.cpp

I double-checked and the header file is saved as a .h file, the main is saved as a .cpp, and I verified again that they're in the same folder...

Any other suggestions? I hate writing classes!
Was This Post Helpful? 0

#6 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: header file issues

Posted 18 September 2010 - 04:58 PM

View PostD_MeNtEd, on 18 September 2010 - 05:55 PM, said:

Sorry about not posting using the code tags, I wasn't sure what they were.

It's not a cin.get() buffer issue.

The full error message is

2 C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\seqsearch.cpp In file included from seqsearch.cpp

I double-checked and the header file is saved as a .h file, the main is saved as a .cpp, and I verified again that they're in the same folder...

Any other suggestions? I hate writing classes!

I never said it was a cin.get(); buffer issue!!!! if you would have read my post i was saying do not use system("pause"); every one hates it and it's horrible i was giving you another way to pause the console GOSH!!!!

This post has been edited by Alex6788: 18 September 2010 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: header file issues

Posted 18 September 2010 - 05:06 PM

Post the entire error and the entire header file. Also, you never call seqSearch(), you call binarySearch().
Was This Post Helpful? 0
  • +
  • -

#8 D_MeNtEd  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 32
  • Joined: 27-August 10

Re: header file issues

Posted 18 September 2010 - 05:19 PM

 
template <class elemType>
int seqSearch (const elemType list[], int length, const elemType& item)

{
    int loc;
    bool found = false;
    
    for (loc = 0; loc < length; loc++)
    {
        if (list[loc] == searchItem)
        {
         found = true;
         break;
         }
}
if (found)
return loc;
else
return -1;
}



Is the entire header file, I changed binarySearch to seqSearch but it's still the same errors.

2 C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\seqsearch.cpp In file included from seqsearch.cpp

C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\seqsearch.h In function `int seqSearch(const elemType*, int, const elemType&)':

10 C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\seqsearch.h `searchItem' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)

C:\Users\Me\Desktop\School Stuff\Quarter 2\Programming 2\Unit 19\Makefile.win [Build Error] [seqsearch.o] Error 1

are the errors

Got it... It was the searchItem in the header file.. and the binarySearch... Was going off an example from the book... not sure where the searchItem came from in the book, but yeah.

Thanks though!
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: header file issues

Posted 18 September 2010 - 05:20 PM

well you should include header guards in it.
#ifndef _SEQSEARCH_H_DEFINED
#define _SEQSEARCH_H_DEFINED

...code...

#endif



to restate what others have guessed, ask you self where searchItem was declared exactly. i think you will find what your looking for :)

This post has been edited by ishkabible: 18 September 2010 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1