Passing Pointers / Iterators to Container

I am going through Lippman's Essential C++ and have some problems with the code.

There is a type mismatch calling find_ver4(). Other probs are similar. Any help appreciated, using VS2010 x64.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;

// globals as convenience for executing procedures
const int int_size = 12;
const int string_size = 4;
const int int_not_present = 1024;

int ia[int_size] =
{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 };
string sa[string_size] =
{ "pooh", "piglet", "eeyore", "tigger" };

vector<int> ivec(ia, ia + int_size);
vector<string> svec(sa, sa + string_size);

template <typename elemType>
inline const elemType* begin( const vector<elemType>& vec )
{
	return vec.empty() ? 0 : &vec[0];
}

template <typename elemType>
const elemType* find_ver4( const elemType* array, int size, 
 const elemType& value )
{
	if (!array || size < 1)
		 return 0;

	// ++array increments array by one element
	for (int ix = 0; ix < size; ++ix, ++array)
		  // *array dereferences the address
		  if (*array == value)
			return array;
	return 0;
}

void prog_find_vers()
{
        const int* iptr = find_ver4( begin( ivec ), ivec.size(), ivec[2] );
	if (iptr != &ivec[2])
		 cerr << "?? find_ver4 failed with int vector!\n";
	else
		 cerr << "!! find_ver4 ok with int vector!\n";

	const string* sptr = find_ver4( begin( svec ), svec.size(), svec[2] );
	if (sptr != &svec[2])
		 cerr << "?? find_ver4 failed with string vector!\n";
	else
		 cerr << "!! find_ver4 ok with string vector!\n";
}

int main()
{
	cerr << "About to execute prog_find_vers()\n\n";
	prog_find_vers();

	return 0; // quiet vc6++
}


source code: http://www.informit.com/store/product.aspx?isbn=0201485184
I'm not sure quite what it's talking about other than parameters don't match. I do know that vector.size() returns an unsigned int and not an int, but that shouldn't cause your error. It might be worth trying since I don't know how type mismatches apply to templates.
std::begin() on a vector returns an iterator (which need not be a pointer). find_ver4() requires a pointer.

1
2
3
4
5
// ...
const int* iptr = find_ver4( &ivec.front() /*begin( ivec )*/, ivec.size(), ivec[2] );
// ...
const string* sptr = find_ver4( &svec.front() /*begin( svec ) */, svec.size(), svec[2] );
// ... 
Thank you.

Volatile Pulse: Ooops. I should have stated that the error was on the first parameter, as stated in the build output. Oh yea. LOTS of warnings on all of the signed / unsigned operations, but true, it did let me continue with the program.

JLBorges: That fix worked, and it allowed me to knock out that chapter. I don't know what the author was trying to do, but I can get back to it later. I couldn't find an errata anywhere on the web.

Solved.
Topic archived. No new replies allowed.