#include <stdint.h>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
void bar(string &s){
std::cout<< s<< std::endl;
}
int main(){
bar("hello");
}
报错原因:
hello作为临时对像是const类型,而bar这个函数的参数是非const类型,所以报错
正确写法:
#include <stdint.h>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
void bar(const string &s){
std::cout<< s<< std::endl;
}
int main(){
bar("hello");
}
本文解释了在C++中向函数传递字符串时遇到的错误,特别是当使用临时对象时,并提供了一个正确的示例来解决该问题。
378

被折叠的 条评论
为什么被折叠?



