您的位置:首页 > 其它

不能将类型为‘std::string&’的非 const 引用初始化为类型为‘const char*’的临时变量

2017-04-01 16:57 471 查看
#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");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐