why wont this run? (small code)

...................................................................................
Last edited on
For starters you are passing a template in a non template function. How can this be?
'vector' is not a type. It's a template. You have to have a vector of something... like vector<int> or vector<float>

Since you're comparing it to searchTerm, (which is an int), you probably want a vector of ints. So:

 
bool BinSearch(const vector<int>& data,int searchTerm)


Note 2 things:

- 'vector<int>' instead of 'vector'
- passing the vector by const reference rather than by value (unrelated to your problem, but a good idea nonetheless)


Also from what I can tell, that function will only work if your vector is sorted.
damn your good.. thanks for the quick replies btw.

now i got 1 last error and cant figure it out.

"Error 1 error C2447: '{' : missing function header (old-style formal list?) c:\users\afattah.pme\documents\visual studio 2008\projects\121\121\121.cpp 6"

Correct, the code only works if the vector is sorted.
Or a vector<T>

1
2
3
4
5
template<T>
bool boBinSearch(const vector<T>&data,int searchTerm)
{
...
}


This way it can work with whatever data your vector is.
Last edited on
Silvermaul:

I thought of that too, but was trying to keep it simple =P

The syntax for that would be this though:

1
2
template <typename T>
bool BinSearch(const vector<T>& data,T searchTerm)


(but then you'd might want to make searchTerm a const reference here since it could be larger objects -- but whatever)



fattah089:

As for your new error -- I can't tell you how to fix without seeing the offending line of code. It sounds like you're defining the function incorrectly.

EDIT:

Also, don't edit your original post once the problem is solved. Leave it there so other people can learn from it. Maybe somebody will come along who has the same problem as you and this thread will help them.

I get help from old forum threads all the time by way of google search.
Last edited on
Disch :

yes you are correct :)
why did the OP deletes his post? don't want to share?
Topic archived. No new replies allowed.