<分区>
为什么这段代码在编译时显示错误?
#include <iostream>
using namespace std;
void foo(int& x){
// cout<<x;
}
int main(){
//int x=3;
foo(3);
return 0;
}
但是通过将参数更改为 const 它可以正确编译
#include <iostream>
using namespace std;
void foo(const int& x){
// cout<<x;
}
int main(){
//int x=3;
foo(3);
return 0;
}
但我仍然传递一个整数,那么如何通过在参数中添加 const 来编译它呢?