vector iterators incompatible

Ok, I've been working on a language parsing program for almost two months now and I think I'm in the home stretch (minus a few minor details). I'm getting a "vector iterators incompatible" error and I am not sure why.

My program has an Orthography class. This class has a vector array of CharacterMatch objects which is basically two strings. One represents input, the other is the output. The idea behind this is if something that is inputted matches a CharacterMatch input in an Orthography, it will return the output.

Here is my Orthography class:

Orthography.h
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
#ifndef ORTHOGRAPHY_H
#define ORTHOGRAPHY_H

#define _CRT_SECURE_NO_WARNINGS

//=====================================================
// Includes
//=====================================================
#include "CharacterMatch.h"
#include <vector>

class Orthography
{
public:

	// Constructor(s)
	Orthography();
	Orthography(const std::wstring& name);

	// Destructor
	~Orthography();

	// Accessor functions
	const std::wstring& GetName() const;
	std::vector<CharacterMatch> GetCharacterMatches() const;

    // Class functions
	bool AddCharacterMatch(const CharacterMatch& match);
	void DeleteCharacterMatch(const std::wstring& matchName);

	// Storage functions
	bool WriteToStream(std::wostream& wos) const;
	bool ReadFromStream(std::wistream& wis);

private:

	std::wstring m_Name;
	std::vector<CharacterMatch> m_CharacterMatches;
	
};


void WriteOrthographiesToFile(const std::vector<Orthography>& orthographies, const std::wstring& filepath);
void ReadOrthographiesFromFile(std::vector<Orthography>& orthographies, const std::wstring& filepath);


#endif // ORTHOGRAPHY_H 


Orthography.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//=====================================================
// Includes
//=====================================================
#include "Orthography.h"
#include <fstream>
using namespace std;

const wchar_t orthoBegin[] = L"BEGIN ORTHOGRAPHY:";
const wchar_t orthoEnd  [] = L"END ORTHOGRAPHY";

// Constructor
Orthography::Orthography()
{
	
}

Orthography::Orthography(const wstring& name) : m_Name(name)
{
	m_Name = name;
}

// Destructor
Orthography::~Orthography()
{

}


// Accessor Functions
const wstring& Orthography::GetName() const
{
	return m_Name;
}

vector<CharacterMatch> Orthography::GetCharacterMatches() const
{
	return m_CharacterMatches;
}


// Class functions
bool Orthography::AddCharacterMatch(const CharacterMatch& match)
{
	// Make sure that the user isn't trying to enter a duplicate match
	vector<CharacterMatch>::iterator iter;
	for (iter = m_CharacterMatches.begin(); iter != m_CharacterMatches.end(); iter++)
	{
		if (iter->GetName() == match.GetName())
		{
			return false;
		}
	}

	m_CharacterMatches.push_back(match);

	return true;
}

void Orthography::DeleteCharacterMatch(const wstring& matchName)
{
	vector<CharacterMatch>::iterator iter;
	for (iter = m_CharacterMatches.begin(); iter != m_CharacterMatches.end(); iter++)
	{
		if (iter->GetName() == matchName)
		{
			m_CharacterMatches.erase(iter);
			break;
		}
	}
}

bool Orthography::WriteToStream(wostream& wos) const
{
	bool ret_val = true;
	wos << orthoBegin << m_Name << endl;
	// from saveOrthographyToFile
	const size_t count = m_CharacterMatches.size();
	for(size_t index = 0; count > index; ++index)
		wos << m_CharacterMatches[index] << endl;
	// end
	wos << orthoEnd << endl;
	return ret_val;
}

bool Orthography::ReadFromStream(wistream& wis)
{
	bool ret_val = true;
	bool got_end = false;
	{
		wstring line;
		getline(wis, line);
		if(0 == line.find(orthoBegin))
			m_Name = line.substr(wcslen(orthoBegin));
		else
			ret_val = false;
	}
	if(ret_val)
	{
		// based on readOrthographyFromFile
		wstring line;
		while(getline(wis, line)) {
			if(line == orthoEnd) {
				got_end = true;
				break;
			}
			CharacterMatch chmatch;
			ret_val = parseStr(line, chmatch);
			if(!ret_val)
				break;
			m_CharacterMatches.push_back(chmatch);
		}
	}
	if(ret_val) {
		ret_val = got_end;
	}
	return ret_val;
}

void ReadOrthographiesFromFile(vector<Orthography>& orthographies, const wstring& filePath)
{
	FILE* fp = _wfopen(filePath.c_str(), L"r,ccs=UNICODE");
	wifstream ifs(fp);

	for( ; ; )
	{
		Orthography orth;
		if(!orth.ReadFromStream(ifs))
			break;
		orthographies.push_back(orth);
	}

	fclose(fp);
}

void WriteOrthographiesToFile(const vector<Orthography>& orthographies, const wstring& filePath)
{
	FILE* fp = _wfopen(filePath.c_str(), L"w,ccs=UNICODE");
	wofstream ofs(fp);

	const size_t count = orthographies.size();
	for(size_t index = 0; count > index; ++index)
	{
		const Orthography& orth = orthographies[index];
		orth.WriteToStream(ofs);
	}

	fclose(fp);
}


Here is my CharacterMatch.h:
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
#ifndef CHARACTER_MATCH_H
#define CHARACTER_MATCH_H

//=====================================================
// Includes
//=====================================================
#include <string>
#include <array>
#include <algorithm>
#include <iostream>


// Used to loop in the parser.  This is the max length of either
// the input or output strings.
const int STR_LEN = 8;


template<size_t N, class V>
std::array<wchar_t, N> toArray(const V& v)
{
    std::array<wchar_t, N> d = std::array<wchar_t, N>();
    copy(std::begin(v), std::end(v), std::begin(d));
    return d;
}

class CharacterMatch
{
public:
	static const size_t N = 8;

	// Constructor(s)
	CharacterMatch()
	: m_Input(std::array<wchar_t, N>()), m_Output(std::array<wchar_t, N>()) {}

	CharacterMatch(const std::wstring& input, const std::wstring& output)
    : m_Input(toArray<N>(input)), m_Output(toArray<N>(output)) {}

	// Copy constructor?
    CharacterMatch(const CharacterMatch& that)
    : m_Input(that.m_Input), m_Output(that.m_Output) {}

	// Destructor
	~CharacterMatch() {}


	// Overloaded = operator
    CharacterMatch& operator=(const CharacterMatch& that)
	{
		m_Input = that.m_Input;
		m_Output = that.m_Output;
        //m_Input = that.m_Output;
        //m_Input = that.m_Output;
        return *this;
    }


	// Accessor functions
	const wchar_t* GetInput() const
	{
        return &m_Input[0];
    }

    const wchar_t* GetOutput() const
	{
        return &m_Output[0];
    }

	std::wstring GetName() const
	{
		std::wstring name = &m_Input[0];
        name += L" - ";
        name += &m_Output[0];
        return name;
	}


private:
	// Members
	std::array<wchar_t, N> m_Input;
    std::array<wchar_t, N> m_Output;

};

inline std::wostream& operator<<(std::wostream& wos, const CharacterMatch& chmatch)
{
    wos << chmatch.GetInput() << L"/" << chmatch.GetOutput();
    return wos;
}

inline bool parseStr(const std::wstring& str, CharacterMatch& chmatch)
{
    size_t pos = str.find(L'/');
    if(pos == str.npos)
        return false;
    std::wstring input  = str.substr(0, pos);
    std::wstring output = str.substr(pos + 1);
    chmatch = CharacterMatch(input, output);
    return true;
}


#endif // CHARACTER_MATCH_H 


In my code, I have this:

1
2
3
4
5
vector<CharacterMatch>::iterator matchIter;
	for (matchIter = orthography.GetCharacterMatches().begin(); matchIter != orthography.GetCharacterMatches().end(); matchIter++)
	{
		
	}


I get the error when the code hits this function. I seem to be able to get through the GetCharacterMatches() function with no problem. But once past it, I get the error. The error also mentions:

include/vector
Line: 238

Anyone have any thoughts?
Orthography::GetCharacterMatches() should probably return a reference.
I've never returned a reference before and I can't seem to get the simple code from the tutorials I've found to work here. How would I go about having that function return a reference?
Hljodulfr wrote:
How would I go about having that function return a reference?


1
2
3
4
const vector<CharacterMatch>& Orthography::GetCharacterMatches() const
{
	return m_CharacterMatches;
}


With a corresponding change to the method declaration in the class.

And, you'll need to use a const_iterator here:
1
2
3
4
5
vector<CharacterMatch>::const_iterator matchIter;
	for (matchIter = orthography.GetCharacterMatches().begin(); matchIter != orthography.GetCharacterMatches().end(); matchIter++)
	{
		
	}
That did it! What I wasn't doing before was making the function const. But I also wouldn't have known to use const_iterator, though.

Thank you!
Topic archived. No new replies allowed.