À l'exercice 8-6 de Accelerated C++, je dois commencer par changer l'implémentation de xref.cc en important un nouveau fichier "split.h" (qui est donné). Le programme renvoie tous les mots donnés en entrée et indique à quelles lignes il les a rencontrés (./xref < xref.cc).

La compilation échoue à la ligne indiquée en commentaire (en capitales). Je ne vous passe pas le message d'erreurs, il est assez inbuvable.

Fichier contenant le main() (xref.cc):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
 
#include "split.h"
 
using std::cin;            using std::cout;
using std::endl;           using std::getline;
using std::istream;        using std::string;
using std::vector;         using std::map;
using std::back_inserter;
 
// find all the lines that refer to each word in the input
map<string, vector<int> >
xref(istream& in,
    void find_words(const string&, vector<string>::iterator) = split) // LIGNE CHANGÉE 
{
	string line;
	int line_number = 0;
	map<string, vector<int> > ret;
 
	// read the next line
	while (getline(in, line)) {
		++line_number;
 
		vector<string> words; // LIGNE CHANGÉE
		// break the input line into words
		find_words(line, back_inserter(words));  // LIGNE CHANGÉE
							// ÇA PLANTE ICI!
 
		// remember that each word occurs on the current line
		for (vector<string>::const_iterator it = words.begin();
		     it != words.end(); ++it)
			ret[*it].push_back(line_number);
	}
	return ret;
}
 
int main()
{
	// call `xref' using `split' by default
	map<string, vector<int> > ret = xref(cin);
 
	// write the results
	for (map<string, vector<int> >::const_iterator it = ret.begin();
	     it != ret.end(); ++it) {
		// write the word
		cout << it->first << " occurs on line(s): ";
 
		// followed by one or more line numbers
		vector<int>::const_iterator line_it = it->second.begin();
 
		cout << *line_it;	// write the first line number
 
		++line_it;
		// write the rest of the line numbers, if any
		while (line_it != it->second.end()) {
			cout << ", " << *line_it;
			++line_it;
		}
		// write a new line to separate each word from the next
		cout << endl;
	}
 
	return 0;
}
Fichier split.h importé:

Code c++ : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <algorithm>
#include <cctype>
#include <string>
 
using std::find_if;
using std::string;
 
#ifndef _MSC_VER
using std::isspace;
#endif
 
inline bool space(char c)
{
        return isspace(c);
}
 
inline bool not_space(char c)
{
        return !isspace(c);
}
 
template <class Out>
void split(const string& str, Out os) {
 
	typedef string::const_iterator iter;
 
	iter i = str.begin();
	while (i != str.end()) {
		// ignore leading blanks
		i = find_if(i, str.end(), not_space);
 
		// find end of next word
		iter j = find_if(i, str.end(), space);
 
		// copy the characters in `[i,' `j)'
		if (i != str.end())
			*os++ = string(i, j);
 
		i = j;
	}
}

Y-a-til un problème au niveau de la déclaration de la fonction find_words?