You program needs a ;
at the end of the line that set the cmd value.
The source code you show has no problem (if you add ;
). We can compile it without warning and it works well, if we add somes lines to make it an autonomous program:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main(int ac, char **av) {
std::string cmd = "par1=1&par2=ciao&par3=1.2e4";
std::stringstream ss(cmd);
std::string argdef;
std::vector<std::string> pairs(0);
while (std::getline(ss, argdef, '&')) {
std::cout << argdef << std::endl;
pairs.push_back(argdef);
}
return 0;
}
For instance, the compilation works with no warning on Linux OpenSuSE LEAP 42.1 with g++/gcc 4.8.5 (the source file is named x.cpp), and the output is correct (as you can see, I've added a line to log the content of argdef, and it looks correct) :
% g++ -Wall -o x x.cpp
% ./x
par1=1
par2=ciao
par3=1.2e4
%
So everything is fine here.
Explanation of the root cause of your segfault error:
- either the
;
you had forgotten on the first line make the program not compile at all, and the binary that you were running was an old one, with bugs;
- or you have anything else in your program that leads to the segmentation fault.
Anyway, the source code shown here is correct.