Hi. I am studying Bjarne Stroustrup's book. He gives the following code while explaining references:
OK. First of all, I notice that if I do this:Code:#include<iostream> #include<vector> #include<string> using namespace std; struct Pair { string name; double val; }; vector<Pair> pairs; double& value(const string& s) { for(int i=0; i<pairs.size(); i++) if(s==pairs[i].name) return pairs[i].val; Pair p={s, 0}; pairs.push_back(p); return pairs[pairs.size()-1].val; } int main() { string buf="aa"; value(buf)++; string buf2="aa"; value(buf2)++; for(vector<Pair>::const_iterator p=pairs.begin(); p!=pairs.end (); ++p) cout<<p->name<<": "<<p->val<< '\n'; cin.get(); return 0; }
I get a compile-time error:Code:double value(const string& s) ...
He speaks of these lvalues here. I don't follow.Code:non-lvalue in increment
Also, I don't quite understand this:
Why is it const? What does this do?Code:...const string& s)
Thanks in advance, Steve



LinkBack URL
About LinkBacks


