Error reading file into an array

I am working in Eclipse, and it keeps giving me this error that I do not understand. In the fillTable function, "is >> kP[i]" Eclipse says: no match for 'operator>>' in 'is >> *(((TranslationTable<int, std::string>*)this)->TranslationTable<int, std::string>::kP + (+(((unsigned int)i) * 8u)))'. And I have no idea what I am doing wrong and what that means. Any suggestions? Thank you.


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
#ifndef TRANSLATIONTABLE_H_
#define TRANSLATIONTABLE_H_
#include "KeyValuePair.h"
#include <iostream>
#include <cstdlib>

template<typename Key, typename Value>
class TranslationTable
{
	private:
		int numPairs;
		KeyValuePair<Key,Value> *kP;

	public:
		TranslationTable();
		TranslationTable(std::istream& is);
		void fillTable(std::istream& is);
		Value getValue(Key myKey) const;
};

template<typename Key, typename Value>
TranslationTable<Key,Value>::TranslationTable()
{
	return;
}

template<typename Key, typename Value>
TranslationTable<Key,Value>::TranslationTable(std::istream& is)
{
	numPairs = 0;
	is >> numPairs;
	kP = new KeyValuePair<Key,Value>[numPairs];
}

template<typename Key, typename Value>
void TranslationTable<Key,Value>::fillTable(std::istream& is)
{
	for(int i = 0; i < numPairs; i++)
	{
		is >> kP[i]; // gives me error here
	}
}

template<typename Key, typename Value>
Value TranslationTable<Key,Value>::getValue(Key myKey) const
{


}

#endif /* TRANSLATIONTABLE_H_ */  
Does KeyValuePair<Key,Value> have overloads for operator>>?
Probably not, because I don't know what you mean? Why would >> not work in this scenario when I want to place the white-space delimited words into the array?
Last edited on
kP is a pointer to KeyValuePair<Key,Value>, so when you reference it with kP[i] and then use >> on it, you are using operator>> with std::istream and KeyValuePair<Key,Value>. Unless you provide a definition of operator>> that works with std::istream and KeyValuePair<Key,Value>, the compiler can't possibly guess what you want.

Define this function:
1
2
3
4
5
6
std::istream &operator>>(std::istream &is, KeyValuePair<Key,Value> &kvp)
{
    //is >> kvp.something;
    //is >> kvp.somethingelse;
    return is;
}
Last edited on
Topic archived. No new replies allowed.