為什么我傳的明明是形參,也能改變xy的值????
#include <iostream>
using namespace std;
/**
?* 定義模板函數swapNum
?* 實現功能:交換兩個數的位置
?*/
template <typename T,int a,int b>
void swap(T )
{
??? T temp = a;
??? a = b;
??? b = temp;
}
int main(void)
{
??? int x = 10;
??? int y = 20;
??? // 調用模板函數
??? swap<int>(x,y);
??????? cout << "x = " << x << endl;
??????? cout << "y = " << y << endl;
?return 0;
}
2020-02-24
你的函數定義的參數表有問題應該是(T a,T b)