Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I'm trying to take a string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}

I'm trying to take a string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}

I'm trying to take a string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}
deleted 1 character in body
Source Link
gsamaras
  • 73.7k
  • 50
  • 210
  • 330

I'm trying to take ana string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}

I'm trying to take an string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}

I'm trying to take a string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}
Post Undeleted by a.sapp
Post Deleted by a.sapp
Source Link
a.sapp
  • 41
  • 1
  • 10

Invalid conversion error when splitting string into a string vector

I'm trying to take an string input from the user, split it into individual strings at each white space and store it in a vector. I'm using the first code snippet from this post (It's the second answer to the question) to do the split on the string. These are the errors I get when compiling:

stringTest.cpp: In function ‘int main()’: stringTest.cpp:23:30: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] split(input, " ", splitInput); ^

stringTest.cpp:8:17: error: initializing argument 2 of ‘std::vector<std::basic_string >& split(std::string&, char, std::vector<std::basic_string >&)’ [-fpermissive] vector &split(string &s, char delim, vector &elems) {

I realize what the first error is saying, but can't figure out what's causing the problem, and I have no idea what the second error means.

This is the code I have written (everything outside of main was taken from the linked post):

#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
vector<string> &split(string &s, char delim, vector<string> &elems) {
 stringstream ss(s);
 string item;
 while (getline(ss, item, delim)) {
 elems.push_back(item);
 }
 return elems;
}
int main(){
 string input;
 cin>>input;
 vector<string> splitInput;
 split(input, " ", splitInput);
 for(int i = 0; i < splitInput.size(); i++){
 cout<< splitInput.at(i) << endl;
 }
 return 0;
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /