c++ - std::stringstream does not name a type (Global) -


i'm doing simulation of few ising models (lattice 1 or -1) , don't want use files (don't gonna details here, guess :d). so, thing is, i'm using stringstreams format data & data's filename separate them bash. show example code

#include <stdio.h> #include <stdlib.h> #include <string.h>     #include <iostream> #include <sstream> #include <string>     using namespace std; void print2bash(string file, string data);  int main(int argc, char *argv[]){      int i, j, n = atoi(argv[1]);     // want put outside main() {     stringstream ssbash;    ssbash.clear(); ssbash.str("");      stringstream ssfile;    ssfile.clear(); ssfile.str("");     //}      //  don't have ss.clear , ss.str("") every time before using them      ssfile << "this_" << n << "_is_the_filename.dat" << endl;     for(i = 0; < n ; i++){         for(j = 0; j < n; j++)             ssbash << i*n+j << "\t";         ssbash << endl;     }      print2bash( ssfile.str(), ssbash.str() );      return 0; } // print2bash(); prints string data; string file;  //               used separate data  //               afterwards ( i.e. ./ising.o <arguments> | bash )   void print2bash(string file, string data){      stringstream ssbash;    ssbash.clear(); ssbash.str(data);            stringstream output;    output.clear(); output.str("");     string line;      output << "for((i=0;i<1;i++)) "<< endl; // buffer bash @ once     while( getline(ssbash,line) )         output << "    echo \"" << line << "\";" << endl;      output << "done >> " << file; // appending     cout << output.str() << endl;      ssbash.clear(); ssbash.str("");      return ; } 

a regular output of "sstreams.o" program be

~work/ising$ ./sstreams.o 5 for((i=0;i<1;i++))      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 >> this_5_is_the_filename.dat 

you idea right? thinking flow, write ./sstreams.o 10 | bash 1 gets nicely separated files. problem is:

i'm getting tired of writing lines each time want use print2bash() (oh irony!)

stringstream ssbash;    ssbash.clear(); ssbash.str("");  stringstream ssfile;    ssfile.clear(); ssfile.str(""); 

because of that, want put ss global variables (don't know if kind of awful compiler doesn't let me it: i'm physicist). so, if write std::stringstream ss variables after #includes (and using namespace std), still error

error: ‘ssbash’ not name type error: ‘ssbash’ not name type error: ‘ssfile’ not name type error: ‘ssfile’ not name type 

from compiler. hope can meee.


edit: [solved]

the problem if wanted declare stringstream out of main() failed because of members clear & str out of context can't used. besides that, use unnecessary because stringstreams created empty contents, @jpalecek , @umlaeute pointed out.

i've declared stringstreams out of main , include clear() + str("") on print2bash() give desired result:

#include <stdio.h> #include <stdlib.h> #include <string.h>     #include <iostream> #include <sstream> #include <string>  #include <vector>  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< vector<int> > chain;    chain.resize(n*n);      for( p = 0; p < np; p++){          ssfile << "chain_p="<< p << "_filename.dat" << endl;         for(i = 0; < n ; i++){             for(j = 0; j < n; j++){                  chain[p].push_back( i*n + j );                 ssbash << i*n+j << "\t";             }             ssbash << endl;        }        print2bash( ssfile.str(), ssbash.str() );     }      return 0; } void print2bash(string file, string data){      ssbash.str(data);     stringstream output;     string line;      output << "for((i=0;i<1;i++)) "<< endl; // buffer bash @ once     while( getline(ssbash,line) )        output << "    echo \"" << line << "\";" << endl;      output << "done >> " << file; // appending      cout << output.str() << endl;      ssfile.clear(); ssfile.str("");     ssbash.clear(); ssbash.str("");      return ; } 

a regular output of ./sstreams.o

~/work/ising$ ./sstreams.o 4 2 for((i=0;i<1;i++))      echo "0 1   2   3   ";     echo "4 5   6   7   ";     echo "8 9   10  11  ";     echo "12    13  14  15  "; done >> chain_p=0_filename.dat  for((i=0;i<1;i++))      echo "0 1   2   3   ";     echo "4 5   6   7   ";     echo "8 9   10  11  ";     echo "12    13  14  15  "; done >> chain_p=1_filename.dat 

thanks everything

you needn't call

ssbash.clear(); ssbash.str(""); 

because stringstream objects automatically created empty contents. leaves with

stringstream ssbash; stringstream ssfile; 

that could global declaration, don't it, because won't you. have clear each time use it.

if don't repeating declaration on , over, can substitute them macro

#define declare_streams \   stringstream ssbash; \   stringstream ssfile; 

or better

#define declare_streams(a, b) \   stringstream a; \   stringstream b; 

used this: declare_streams(ssbash, ssfile).

but best (and cleanest) solution refactor code formats data single function. way, wouldn't have worry repeating stream declaration.

btw why instead of writing file c++ emit script writing beyond me.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -