Range For Missing Begin Function

My program does not compile because of errors in boldfaced, lines 73 and 83. How do I fix?

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
101
102
103
104
105
106
107
// C23TryThis.cpp
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include <map>
using namespace std;

typedef vector<string>::const_iterator Line_iter;

class Message { // a Message points to the first and last lines of a message
	Line_iter first;
	Line_iter last;
public:
	Message(Line_iter p1, Line_iter p2) : first{ p1 }, last{ p2 } {}
	Line_iter begin() const { return first; }
	Line_iter end() const { return last; }
	//...
};

using Mess_iter = vector<Message>::const_iterator;

	struct Mail_file {		// a Mail_file holds all the lines from a file
							// and simplifies access to messages
	string name;			// file name
	vector<string> lines;	// the lines in order
	vector<Message> m;		// Messages in order

	Mail_file(const string& n);		//	read file n into lines
	
	Mess_iter begin() const { return m.begin(); }
	Mess_iter end() const { return m.end(); }
};

bool find_from_addr(const Message* m, string& s);

string find_subject(const Message* m);



int main()
{
	Mail_file mfile{ "my-mail-file.txt" };

	//	first gather messages from each sender together in a multimap:

	multimap<string, const Message*> sender;

	for (const auto& m : mfile) {
		string s;
		if (find_from_addr(&m, s))
			sender.insert(make_pair(s, &m));
	}

	//	now iterate through the multimap
	//	and extract the subject of John Doe's messages:
	auto pp = sender.equal_range("John Doe <jdoe@machine.examples>");
	for (auto p = pp.first; p != pp.second; ++p)
		cout << find_subject(p->second) << '\n';
}

int is_prefix(const string& s, const string& p)
		// is p the first part of s?
{
	int n = p.size();
	if (string(s, 0, n) == p) return n;
	return 0;
}

bool find_from_addr(const Message* m, string& s)
{
	for (const auto& x : m)  //Error: this range based 'for' statement requires a suitable "begin" function and none was found
		if (int n = is_prefix(x, "From: ")) {
			s = string(x, n);
			return true;
		}
	return false;
}

string find_subject(const Message* m)                                                                                               m)
{
	for (const auto& x : m) //Error: expected a declaration
		if (int n = is_prefix(x, "Subject: ")) return string(x, n);
	
	return "";
}

Mail_file::Mail_file(const string& n)
{
	ifstream in{ n };
	if (!in) {
		cerr << "no " << n << '\n';
		exit(1);
	}

	for (string s; getline(in, s);)
		lines.push_back(s);

	auto first = lines.begin();
	for (auto p = lines.begin(); p != lines.end(); ++p) {
		if (*p == "----") {
			m.push_back(Message(first, p));
			first = p + 1;
		}
	}
}


Here is my data in my-mail-file.txt
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
xxx
xxx
From: John Doe <jdoe@machine.example>
To: Mary Smith <mary@example.net>
Subject: Saying Hello
Date: Fri, 21 Nov 1997 09:55:06 - 0600
Message-ID: <1234@local.machine.example>

This is a message just to say hello.
So, "Hello".
----
From: Joe Q. Public <john.q.public@example.com>
To: Mary Smith <@machine.tld:mary@example.net>,, jdoe@test .example
Date: Tue, 1 Jul 2003 10:52:37 +0200
Message-ID: <5678.21-Nov-1997@example.com>

Hi everyone.
----
To: "Mary Smith: Personnal Account" <smith@home.example>
From: John Doe <jdoe@machine.example>
Subject: Re: Saying Hello
Date: Fri, 21 Nov 1997 11:00:00 - 0600
Message-ID: <abcd.1234@local.machine.tld>
In-Reply-To: <1456@example.net>
References: <1234@local.machine.example> <3456@example.net>

This is a reply to your reply.
----
----
Last edited on
Write your question here.

You seem to have forgotten that part.

Pass your Message objects by reference. There is no need for pointers here. If, for some reason you must use pointers, dereference the pointer when it's used in a ranged for loop.
Cire,

I passed my Message objects by reference in lines 73 and 83. The errors in lines 73 and 83 detected by the IDE went away. However, when I compiled the program, I received two error messages.

c23trythis.cpp(82): error C3646: 'm' : unknown override specifier
c23trythis.cpp(82): error C2059: syntax error : ')'

How do I get rid of the errors?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool find_from_addr(const Message& m, string& s)
{
	for (const auto& x : m)
		if (int n = is_prefix(x, "From: ")) {
			s = string(x, n);
			return true;
		}
	return false;
}

string find_subject(const Message& m)                                                                                               m)
{
	for (const auto& x : m)
		if (int n = is_prefix(x, "Subject: ")) return string(x, n);
	
	return "";
}
Take a look at the m) at the end of line 11.
I deleted m) at the end of line 11 and retyped m). Errors did not go away.
string find_subject(const Message& m) m) // <---- remove this


Sorry, coder777, but neither lines 39 and 81 in my original post nor line 11 in my second post has the extra m).
Both lines 81 in the original post and 11 in your second post have an extra m). Look far to the right. It's the reason your code block is so wide.
Thanks cire and coder777. Looks like my peripheral vision is poor. The program compiles and runs.
Topic archived. No new replies allowed.