Symptom:
Today, I wrote a piece of code like this.
vector<string> blankVector(){ return vector<string> blankVector;}
My original intention was to combine the vector <string> blankVector; return blankVector into one sentence. The result is a natural compilation error.
Lujun9972 @ X41 :~ /Study/c/test $ make
G ++-L/usr/lib-o test. cpp
Test. cpp: In the function 'std: vector <std: basic_string <char> blankVector:
Test. cpp: 27: 27: Error: expected primary-expression before 'blance'
Test. cpp: 27: 27: Error: expected '; 'before' blankVector'
Make: *** [test] Error 1
Solution:
In this case, when the C language compiler processes the value assignment statement (the value after the return statement is assigned to the return value, it seems that its right value is expected to be an object rather than a definition..In fact, this code should be modified
vector<string> blankVector(){ return vector<string>();}
Herevector<string>()
An anonymous object is generated, and the return Statement is assigned the return value.
Similarly, you can use this method to initialize a class member variable, for example
//A.hclass A{static map<string,string> mss;}//A.cppmap<string,string> A::mss = map<string,string>();
This article is from the "dark day" blog, please be sure to keep this source http://darksun.blog.51cto.com/3874064/1264760