Using Classes

In Chapter 6 of Beginning C++ Programming, I followed the instructions to code a search program. However, I still have error despite going over the code several times.

Please resolve the errors in line 59, 66, and 71.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Using Classes
#include <iostream>
#include <string>
#include <io.h>

using namespace std;

// class that encapsulate searching for a file with a pattern
// using the C runtime functions
	class search_handle
	{
		// this is the data member that will be managed
		intptr_t handle;
		// public here means public to the containing class
	public:
		search_handle() : handle(-1) {}
		search_handle(intptr_t p) : handle(p) {}
		void operator=(intptr_t p) { handle = p; }
		// DO NOT ALLOW COPYING
		search_handle(search_handle& h) = delete;
		void operator=(search_handle& h) = delete;
		// move sematics allowed
		search_handle(search_handle&& h) { close(); handle = h.handle; }
		void operator=(search_handle&& h) { close(); handle = h.handle; }
		// allow implicit conversion to a bool no check that the
		// data member is valid
		operator bool() const { return (handle != -1); }
		// allow implicit conversion to an intptr_t
		operator intptr_t() const { return handle; }
		// allow callers to explicitly release the handle
		void close() { if (handle != -1) _findclose(handle); handle = 0; }
		// allow automatic release of the handle
		~search_handle() { close(); }
	};

	class file_search
	{
		// class used to manage the lifetime of a search handle
		// used by the C runtime _findfirst/_findnext functions
	search_handle handle;
	string search;

	// these are the public interface of the class
public:
	// we can create objects with a C or C++ string
	file_search(const char* str) : search(str) {}
	file_search(const string& str) : search(str) {}
	// get access to the search string		
	const char* path() const { return search.c_str(); }
	// explicitly close the search
	void close() { handle.close(); }
	// get the next value in the search
	bool next(string& ret)
	{
		_finddata_t find();
		if (!handle)
		{
			// the first call to get the search handle
			handle = _findfirst(search.c_str(), &find);
			if (!handle)
				return false;
		}
		else
		{
			// subsequent calls on the search handle
			if (-1 == _findnext(handle, &find))
				return false;
		}

		// return the name of the file found
		ret = find.name;
		return true;
	}
};

void usage()
{
	cout << "usage: search pattern" << endl;
	cout << "pattern is the file or folder to search for "
		<< "with or without wildcards * and ?" << endl;
}

int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		usage();
		return 1;
	}

	file_search files(argv[1]);
	cout << "searching for " << files.path() << endl;
	string file;
	while (files.next(file))
	{
		cout << file << endl;
	}

	return 0;
}


Last edited on
Tell us the error.
line 59 - error C2664: has to do with intptr _findfirst
line 66 - error C2664: has to do with int _findnext
line 71 - error C2228: left of '.name' must have a class/struct/union

I am using Microsoft Visual Studio 2017, the free download.
Line 55: _finddata_t find();
This declares a function called find which accepts no arguments and has the return type _finddata_t

Change it to: _finddata_t find{};
This would declare a variable of type _finddata_t, with the object being zero initialised.
This is what you want.
Hi JL,

Thank you very much. What a difference braces make!!!
Registered users can post here. Sign in or register to post.