Error C2679

I've searched google and tried a few things myself.. and I've found nothing and I know its a simple fix. Can someone please point out what I'm over looking? ><

VS2013..
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


void WriteToFile();
void ReadFromFile();
void LoadMap(vector<string> &mapdata);
void PrintMap(vector<string> mapdata);
int main()
{
	vector<string> Map;
	//WriteToFile();
	//ReadFromFile();

	LoadMap(Map);
	PrintMap(Map);



	system("PAUSE");
	return 0;
}

void WriteToFile()
{
	ofstream file;

	file.open("MyFile.txt");
	if (file.fail())
	{
		cout << "ERROR: Could not open file!" << endl;
		perror("MyFile.txt");
		cout << "ERROR: Could not open file!" << endl;
	}

	file << "Map:test" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;

	file.close();

	cout << "Writing to file was a Succses!" << endl;
}

void ReadFromFile()
{
	ifstream file;

	file.open("MyFile.txt");
	if (file.fail())
	{
		cout << "ERROR: Could not open file!" << endl;
		perror("MyFile.txt");
		cout << "ERROR: Could not open file!" << endl;
	}

	string lineContent;

	while (getline(file, lineContent))
	{
		cout << lineContent << endl;
	}
	cout << endl;

	file.close();

	cout << "Reading from file was a Succses!" << endl;
}

void LoadMap(vector<string> &mapdata)
{
	ifstream file;

	file.open("MyFile.txt");
	if (file.fail())
	{
		cout << "ERROR: Could not open file!" << endl;
		perror("MyFile.txt");
		cout << "ERROR: Could not open file!" << endl;
	}

	string lineContent;

	while (getline(file, lineContent))
	{
		mapdata.push_back(lineContent);
	}

	file.close();
}

void PrintMap(vector<string> mapdata)
{
	vector<string> temp = mapdata;
	for (auto i = temp.begin(); i != temp.end(); ++i) 
	{
		//printf("%s\n", temp[i].c_str());
		cout << temp << endl; //<=== ERROR
		cout << temp[i] << endl; //<=== ERROR
		
	}
}
closed account (SECMoG1T)
Sure there is an error

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
void PrintMap(vector<string> mapdata)
{
	vector<string> temp = mapdata;/// temp is a copy mapdata{vector}
	for (auto i = temp.begin(); i != temp.end(); ++i) ///auto i , 'i' is an iterator
	{
		//printf("%s\n", temp[i].c_str());
		cout << temp << endl; ///vectors have no overloaded << operator because they
                                                    /// are generic containers
		cout << temp[i] << endl; /// i is an iterator not an integral index.
		
	}
}


void PrintMap(const vector<string>& mapdata)///copying vectors can be costly at times so prefer const reference to passing by value.
{
	///our vector is passed as an immutable object hence we dont need a copy here.
	for (auto i = mapdata.begin(); i != mapdata.end(); ++i) 
	{
		//printf("%s\n", (*i).c_str()); could have been the right way
		///cout << temp << endl; you dont need this
		cout << *i << endl; //what you needed.
		
	}
}


Edit: Also there is a logical bug on how you handle error checking on some of your files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void WriteToFile()
{
	ofstream file;

	file.open("MyFile.txt");
	if (file.fail())
	{
		cout << "ERROR: Could not open file!" << endl;
		perror("MyFile.txt");
		cout << "ERROR: Could not open file!" << endl;
	}///what happens when we get here , i guess execution continues and all operataions below would be invalid, advice: if the check above was true simply exit this 
///function or use better error handling mechanism.

	file << "Map:test" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;
	file << "XXX" << endl;

	file.close();

	cout << "Writing to file was a Succses!" << endl;
}

Last edited on
Thanks so much! so all I needed to do was a pointer to the "i" address
Would this work for some good error handling?

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
void LoadMap(vector<string> &mapData, string fileName)
{
	ifstream file;

	file.open(fileName);
	if (file.fail())
	{
		cout << "===========================" << endl;
		cout << "ERROR: Could not open file!\n" << endl;
		perror(fileName.c_str());
		cout << "===========================" << endl;
		system("PAUSE");
		exit(1);
	}

	string lineContent;

	while (getline(file, lineContent))
	{
		mapData.push_back(lineContent);
	}

	file.close();
	cout << "Loading Map was a Succses!\n" << endl;
}
Last edited on
closed account (SECMoG1T)
iterators do provide operations similar to pointers, so the star operator is used for derefencing iterators ,{*i} dereferences iterator i.

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
void LoadMap(vector<string> &mapData, string fileName)
{
	ifstream file(fileName);///prefer constructors to the open function, that way you wouldn't need to close the stream
	if (!file)///similarly use streams direcly to check for a wider range of errors
	{
		cout << "===========================" << endl;
		cout << "ERROR: Could not open file!\n" << endl;
		perror(fileName.c_str());
		cout << "===========================" << endl;
		///system("PAUSE"); avoid system calls as much as possible they are very slow
		return;//better
	}

	string lineContent;

	while (getline(file, lineContent))
	{
		mapData.push_back(lineContent);
	}

	///file.close(); not needed
	cout << "Loading Map was a Succses!\n" << endl;
}

	
Last edited on
Topic archived. No new replies allowed.