Questions about operator[]

Hi,guys;

This is an exemple of chapter 11 section 8, Subscripting,The C++ Program Language Special Edition,

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
#include <iostream>
#include <iterator>
#include <vector>
#include <string.h>
#include <algorithm>
using namespace std;

class Assoc{
		struct Pair{
		string name;
		double value;
		Pair(string n="", double v=0):name(n),value(v) {}
		};
		
		vector<Pair> vec;

		Assoc(const Assoc&);
		Assoc& operator=(const Assoc&);
	public:
		Assoc(){};
		const double& operator[] (const string&);
		double& operator[] (string&);
		void print_all() const;
	};

double& Assoc::operator[] (string& s){

	for(vector<Pair>::const_iterator it = vec.begin();it != vec.end();++it)
		if(s==it->name) return it->value;
	vec.push_back(Pair(s,0));
	return vec.back().value;
}

void print_all(){

	for(vector<Pair>::const_iterator p= vec.begin(); p != vec.end();++p)
		std::cout<<p->name<<" : "<<p->value;
}

int main()
{

	string buf;
	Assoc vec;
	while(cin>>buf)vec[buf]++;
	vec.print_all();

	
	return 0;
}


When I run this program, it have some problems:

One is :

The complier find errors, one of them is
 
error C2784:"" bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)" can't get the "const std::vector<_Ty,_Ax> &" from the template argument of "std::string""

Did I miss any head files in this program or another errors?

The next is:
struct Pair is defined in class Assoc. Does it waste same one memory space in every instance?

question 3:
why defined two funcion of operator[], one is const, is it necessary?

question 4:
This instruction
 
while(cin>>buf)vec[buf]++;

The "vec[buf]" returned the reference of value of vec[buf] , and the operator++ is not defined in class Assoc.
Can this instruction correct?
if it runs, then this instruction modified the members of class Assoc, is it? and is it correct?

Thanks help!
Hi guy,

I can't reproduce your compiler error using g++ (GCC) 3.4.5 (mingw special).

Anyway, your code will not work, because
1) line 34 should read
void Assoc::print_all() const {
2) in the non-const operator[] you have to use a non-const iterator. The g++ gives a more reasonable error message:
error: invalid initialization of reference of type 'double&' from expression of type 'const double'

Does it work now?
Thanks dwk
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
#include <iostream>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;

class Assoc{
		struct Pair{
		string name;
		double value;
		Pair(string n="", double v=0):name(n),value(v) {}
		};
		
		vector<Pair> vec;

		Assoc(const Assoc&);
		Assoc& operator=(const Assoc&);
	public:
		Assoc(){};
		const double& operator[] (const string&);
		double& operator[] (string&);
		void print_all() const;
	};

double& Assoc::operator[] (string& s){

	for(vector<Pair>::iterator it = vec.begin();it != vec.end();++it)
		if( s == it->name) return it->value;
	vec.push_back(Pair(s,0));
	return vec.back().value;
}

void Assoc::print_all() const {

	for(vector<Pair>::const_iterator p= vec.begin(); p != vec.end();++p)
		std::cout<<p->name<<" : "<<p->value;
}


int main(int argc, char *argv[])
{
  
	string buf;
	Assoc vec;
	while(cin>>buf)vec[buf]++;
	vec.print_all();

  system("PAUSE");	
  return 0;
}


It works when I use the dev-c++(v4.9.8) complimer of bloodshed software.

but it still doesn't work under the VS2003.

same error:
error C2784: "bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &) "could not get "const std::vector<_Ty,_Ax> &" from "std::string";

I thought there is a ambiguity of operator== since I missed some head files.

Is anyone can help me about Virual Studio C++?

and the questions that I asked last time is not be answered.

any one can help me too.

question 1:
How the complier deal with the define of struct Pair, is it waste memorey space of every instance?

question 2:
Why defined two function of operator[], and one is const?
1
2
const double& operator[] (const string&);
double& operator[] (string&);


question 3:
Why the instruction
 
vec[buf]++;

can modify the value of Assoc's member?
Last edited on
Hi!
Finally got the source of your compiler error:
Don't include string.h but only string:
 
#include <string> 

For MS-Copiler this makes a difference... (tested on MSVC++ 7.1.3088)
And te iterator in line 28 must not be const.

Reganding your other questions:

ad 1): The definitoin of Struct as an inner class to Assoc doesn't waste any space at runtime, because it is like a type definition. the struct Pair {...} doesn't generate executable code. btw, I don't see the reason fpr definig Pair on your own - std::pair<std::string, double> does basically the same.

ad 2) You have a const and a non-const version of the the [] operator. This is important if you use the [] operator inside a method which is defined const itself. In order to work your declaration shall be
const double& operator[] (const string&) const ; and you have to implement it, of course. The compiler will then pick the const operator if the this -pointer ist const and the volatile version otherwise.

ad 3) Yes, definitely. this is obviously the non-const [] operator...

Try it out by placing this
1
2
	std::string key("johndoe");
	std::cout << (*this)[key] << std::endl;

in the printAll method while not having implemented the const-version of the [] operator - the compiler will complain!
Last edited on
:)

haaa.

Thanks dwk!

I found the problem at the same time!

I included the sting.h file, but it must be the sring file!

haaaa.....

where are you, dwk? thank you for your help.again!

Topic archived. No new replies allowed.