Wednesday, 15 May 2013

c++ - stringstream operator>> fails as function, but works as instance? -


I am writing a simple code that will remove a bunch of names, int pairs from a file. I am modifying the existing code which uses just:

  string chrome; Unsigned full size;  

But I want to use a second (similar) input file in which the first two columns are the same, but after the other data (which will be ignored). So I write:

  string chrome; Unsigned full size; String line; (Getline (CIN, line)) {if (stringstream (line)> chrom & gt; size) {// save values}}  

But failing to compile this typical porn study gives Lib Template template:

  error: "operator"> gt; & Gt; "Std :: basic_stringstream"; four; std :: char_traits> four>, std :: communicator> four>> (((constants std :: basic_string & lt; four, std :: char_traits & lt; four & gt; std :: communicator & lt; four & gt; & amp;; ((static std :: basic_string & lt; four; std :: char_traits & lt; four & Gt ;, std :: communicator & lt; four & gt; & gt; *) (& amp; line)), std :: operator | (_S_out, _S_in))>> chrome "isstream: 131 : Note: Candidates are: std :: basic_istream & lt; _CharT, _Traits & gt; & Amp; Std :: basic_istream & lt; _CharT, _Traits & gt; :: Operator & gt; & Gt; (Std :: basic_istream & lt; _CharT, _Traits & gt; & amp; (*) (std :: basic_istream & lt; _CharT, _Traits & gt; & amp;)) [with _CharT = four, _Traits = std :: Char_traits & lt; Char & gt;] [... another dozen lines ...]  

The correct line is not std :: string, but some variations of std :: basic_string, etc. However, to explicitly trigger stringstream functions

  string chrome; Unsigned full size; String line; While (gateline (genome, line)) {stringstream ss (line); If (SS>> Chrome> Size) {// save value} // delete the rest of the line //}  

Why? What is wrong in the first case? Always useful works at cplusplus.com, why is not my code?

Update: Another point of reference: The temporary stringstream works when the first value is extracted is an integer instead of a string:

  unsigned int chrom; // serves as integer ... unsigned full size; String line; While (getline (CIN, line)) {if (stringstream (line)> chrom & gt; size) {// save values}}  

Give over three groups of member tasks and a group of global actions on "extraction operator" (+)

  • String Stream (line); - A floating object
  • stringstream SS (line); - A common object.

When "chrome" is an integer, operator is more overloaded as arithmetic. Extractor which is a member function is normal object or temporary object is both fine.

When "chrome" is the string, operator istream & amp; Operator & gt; & Gt; (Istream & is, char * str) , it is a global task that should reference the object as a parameter. However, the temporary object has been given, we do not have permission to pass temporary objects from the non-conferencing context in standard C ++. Overload function can not get reference of floating object unless the overload function is istream & amp; Operator & gt; & Gt; (The cost is and is, four * string) Unfortunately, this is not the fact. In case of temporary objects, the function (s) can not be overloaded, and therefore the error: there is no match for the function ...


No comments:

Post a Comment