C++:6、分析下列程序的错误

来源:百度知道 编辑:UC知道 时间:2024/06/04 04:52:27
#include<iostream>
using namespace std;
void Swap(int a, int b);
void Swap(int& a, int& b);
void main()
{
int x(5), y(10);
cout<<"x="<<x<<" y="<<y<<endl;
Swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
}
void Swap(int& a, int& b)
{
int t;
t=a;
a=b;
b=t;
}
void Swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
}

#include <iostream>
//using namespace std;
using std::cout;
using std::endl;

//void Swap(int a, int b);
void Swap(int& a, int& b);

void main()
{
int x(5), y(10);
cout<<"x="<<x<<" y="<<y<<endl;
Swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
}
void Swap(int& a, int& b)
{
int t;
t=a;
a=b;
b=t;
}
/*void Swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
}*/

以上两个函数的参数都是按值传递,c++的重载函数调用机制区分不开。

Swap(x,y);这个你调用的时候引起二义性了,所以得把其中一个函数名改了才行。