#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
template<class T>
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
void test1(){
int a=1,b=2;
// mySwap(a,b);
mySwap(1,2);//报错哦############################
cout<<a<<" "<<b<<endl;
float c=1.1,d=2.2;
mySwap<float>(c,d);
cout<<c<<" "<<d<<endl;
}
int main(int argc, char** argv) {
test1();
return 0;
}
C:\Users\Administrator\Desktop\C++\main.cpp [Error] invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'
模板这里不能直接用常量,用变量。改成上面一行的内容就不报错了~