Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::stringstream does not name a type (Global)
    text
    copied!<p>I'm doing a simulation of a few Ising models (lattice with 1 or -1) and I don't want to use files (don't gonna get into details here, I guess :D). So, the thing is, I'm using stringstreams to format the data &amp; data's filename to then separate them with bash. Show you an example code</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; using namespace std; void print2bash(string file, string data); int main(int argc, char *argv[]){ int i, j, n = atoi(argv[1]); // I want to put this outside main() { stringstream ssbash; ssbash.clear(); ssbash.str(""); stringstream ssfile; ssfile.clear(); ssfile.str(""); //} // So I don't have to do ss.clear and ss.str("") every time before using them ssfile &lt;&lt; "this_" &lt;&lt; n &lt;&lt; "_is_the_filename.dat" &lt;&lt; endl; for(i = 0; i &lt; n ; i++){ for(j = 0; j &lt; n; j++) ssbash &lt;&lt; i*n+j &lt;&lt; "\t"; ssbash &lt;&lt; endl; } print2bash( ssfile.str(), ssbash.str() ); return 0; } // print2bash(); prints string data; to string file; // This will be used to separate data // afterwards ( i.e. ./Ising.o &lt;arguments&gt; | bash ) void print2bash(string file, string data){ stringstream ssbash; ssbash.clear(); ssbash.str(data); stringstream output; output.clear(); output.str(""); string line; output &lt;&lt; "for((i=0;i&lt;1;i++)) do "&lt;&lt; endl; // Buffer all to bash at once while( getline(ssbash,line) ) output &lt;&lt; " echo \"" &lt;&lt; line &lt;&lt; "\";" &lt;&lt; endl; output &lt;&lt; "done &gt;&gt; " &lt;&lt; file; // Appending cout &lt;&lt; output.str() &lt;&lt; endl; ssbash.clear(); ssbash.str(""); return ; } </code></pre> <p>A regular output of this "sstreams.o" program would be</p> <pre><code>~work/Ising$ ./sstreams.o 5 for((i=0;i&lt;1;i++)) do echo "0 1 2 3 4 "; echo "5 6 7 8 9 "; echo "10 11 12 13 14 "; echo "15 16 17 18 19 "; echo "20 21 22 23 24 "; done &gt;&gt; this_5_is_the_filename.dat </code></pre> <p>You get the idea right? From this thinking flow, what I actually write is <code>./sstreams.o 10 | bash</code> so one gets nicely separated files. But the problem is:</p> <p><strong>I'm getting tired of writing this lines</strong> each time I want to use print2bash() (oh the irony!)</p> <pre><code>stringstream ssbash; ssbash.clear(); ssbash.str(""); stringstream ssfile; ssfile.clear(); ssfile.str(""); </code></pre> <p>Because of that, I want to put the <code>ss</code> as global variables (don't know if this is that kind of awful so the compiler doesn't let me to do it: I'm just a Physicist). So, even if I write <code>std::stringstream</code> for the ss variables after the <code>#include</code>s (and <code>using namespace std</code>), I still get the error</p> <pre><code>error: ‘ssbash’ does not name a type error: ‘ssbash’ does not name a type error: ‘ssfile’ does not name a type error: ‘ssfile’ does not name a type </code></pre> <p>from the compiler. Hope you can help meee.</p> <hr> <p>EDIT: <strong>[SOLVED]</strong></p> <p>The problem was that if I wanted to declare a <code>stringstream</code> out of <code>main()</code> it failed because of the members <code>clear</code> &amp; <code>str</code> where out of context so they can't be used. Besides that, this use was unnecessary because <code>stringstream</code>s are created with empty contents, as @jpalecek and @umlaeute pointed out. </p> <p>I've declared the <code>stringstream</code>s out of main and include a <code>clear()</code> + <code>str("")</code> on <code>print2bash()</code> to give the desired result:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; stringstream ssbash, ssfile; void print2bash(string file, string data); int main(int argc, char *argv[]){ int i, j, n = atoi(argv[1]), p, np = atoi(argv[2]); vector&lt; vector&lt;int&gt; &gt; chain; chain.resize(n*n); for( p = 0; p &lt; np; p++){ ssfile &lt;&lt; "chain_p="&lt;&lt; p &lt;&lt; "_filename.dat" &lt;&lt; endl; for(i = 0; i &lt; n ; i++){ for(j = 0; j &lt; n; j++){ chain[p].push_back( i*n + j ); ssbash &lt;&lt; i*n+j &lt;&lt; "\t"; } ssbash &lt;&lt; endl; } print2bash( ssfile.str(), ssbash.str() ); } return 0; } void print2bash(string file, string data){ ssbash.str(data); stringstream output; string line; output &lt;&lt; "for((i=0;i&lt;1;i++)) do "&lt;&lt; endl; // Buffer all to bash at once while( getline(ssbash,line) ) output &lt;&lt; " echo \"" &lt;&lt; line &lt;&lt; "\";" &lt;&lt; endl; output &lt;&lt; "done &gt;&gt; " &lt;&lt; file; // Appending cout &lt;&lt; output.str() &lt;&lt; endl; ssfile.clear(); ssfile.str(""); ssbash.clear(); ssbash.str(""); return ; } </code></pre> <p>a regular output of <code>./sstreams.o</code></p> <pre><code>~/work/Ising$ ./sstreams.o 4 2 for((i=0;i&lt;1;i++)) do echo "0 1 2 3 "; echo "4 5 6 7 "; echo "8 9 10 11 "; echo "12 13 14 15 "; done &gt;&gt; chain_p=0_filename.dat for((i=0;i&lt;1;i++)) do echo "0 1 2 3 "; echo "4 5 6 7 "; echo "8 9 10 11 "; echo "12 13 14 15 "; done &gt;&gt; chain_p=1_filename.dat </code></pre> <p>Thanks for everything</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload