#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");
}