-1

I want to reuse istringstream variable. It is easy to initial istringstream variable using construction function. But when I try to re-assign it to a new value using = or <<, I got error. I am using c++11. It seems no compile error in vs 2015 using =. But gcc 4.8.2 (devtools-2 on Centos 6.4 x86_64) get error.

Codes like below:

std::string line;

int simmulationtimes; double InitialIDensity;

// deploy configuration file
std::ifstream config(configfile);
if (!config.is_open())
{
       return false;
}
std::getline(config, line);
std::istringstream sline(line);
std::string sInitialIDensity;
while(std::getline(sline, sInitialIDensity, '=')); 
InitialIDensity = std::stod(sInitialIDensity);

std::getline(config, line);
std::string ssimmulationtimes;
sline.str("");  sline.clear();
sline = std::istringstream(line);
while (std::getline(sline, ssimmulationtimes, '='));
simmulationtimes = std::stoi(ssimmulationtimes);

configuration file should be:

IDensity=0.5
times=5

Error:

error: no match for ‘operator=’ (operand types are ‘std::istringstream {aka std::basic_istringstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
  sline = std::istringstream(line);

Solutions (like how-to-initialize-a-stdstringstream) on stackoverflow about reusing stringstream not work for me. Any ideas about reusing istringstream? Thanks for your consideration.

Community
  • 1
  • 1
Nick Dong
  • 3,638
  • 8
  • 47
  • 84

1 Answers1

1

Use the str() method to set a new std::string into an existing std::istringstream.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • It seems no compile error in vs 2015 using =. But gcc 4.8.2 (devtools-2 on Centos 6.4 x86_64) get error. – Nick Dong Oct 22 '16 at 15:53