[Error] cannot convert 'std::string {aka std::basic_string} to 'char' in assignment

在做c++字符串反转时,出现报错“[Error] cannot convert 'std::string {aka std::basic_string} to 'char' in assignment”

源代码如下:

#include
#include
using namespace std;
int main(void){
	string str = "";
	string t = "";
    cin>>str;
    int index = str.size(); 
    for(int i = 0;i < str.size() / 2;i++){
        t = str[i];
        str[i] = str[index - 1];
        str[index - 1] = t;
        index --;
    }
    cout<

修改方法:

把string t = "" 替换为 char t = ' ' (注意要有一个空格)

修改后的代码:

#include
#include
using namespace std;
int main(void){
	string str = "";
	char t = ' ';
    cin>>str;
    int index = str.size(); 
    for(int i = 0;i < str.size() / 2;i++){
        t = str[i];
        str[i] = str[index - 1];
        str[index - 1] = t;
        index --;
    }
    cout<

程序正常运行通过!

你可能感兴趣的:(焊bug路漫漫)