I just started using C++, and im currently working on using references to do multiple returns.
This sample code simply swaps two ints:
[cpp]
#include <iostream>
using namespace std;
int main()
{
int x =12;
int y =21;
cout << "x is: " << x <<endl;
cout << "y is: " << y << endl;
swap(x,y);
cout << "new x is: " << x << endl;
cout << "new y is: " << y << endl;
return 0;
}
void swap(int &rX, int &rY)
{
int temp;
temp = rX;
rX = rY;
rY = temp;
}
[/cpp]
This code works fine, they get swapped and all is happy.
However, when i try doing the same thing but swapping 3 ints i get a compiler error "basic_ref.cpp:15: error: no matching function for call to ‘swap(int&, int&, int&)'". Is there a limit to the number of references i can pass into a function? Or am i just doing something wrong with my code?
[cpp]
#include <iostream>
using namespace std;
int main()
{
int x =12;
int y =21;
int z = 17;
cout << "x is: " << x <<endl;
cout << "y is: " << y << endl;
cout << "z is: " << z << endl;
swap(x,y,z);
cout << "new x is: " << x << endl;
cout << "new y is: " << y << endl;
cout << "new z is: " << z << endl;
return 0;
}
void swap(int &rX, int &rY, int &rZ)
{
int temp;
temp = rX;
rX = rY;
rY = rZ;
rZ = temp;
}
[/cpp]
BTW, i am using the GCC 4.4.1 compiler if that makes any difference.
This sample code simply swaps two ints:
[cpp]
#include <iostream>
using namespace std;
int main()
{
int x =12;
int y =21;
cout << "x is: " << x <<endl;
cout << "y is: " << y << endl;
swap(x,y);
cout << "new x is: " << x << endl;
cout << "new y is: " << y << endl;
return 0;
}
void swap(int &rX, int &rY)
{
int temp;
temp = rX;
rX = rY;
rY = temp;
}
[/cpp]
This code works fine, they get swapped and all is happy.
However, when i try doing the same thing but swapping 3 ints i get a compiler error "basic_ref.cpp:15: error: no matching function for call to ‘swap(int&, int&, int&)'". Is there a limit to the number of references i can pass into a function? Or am i just doing something wrong with my code?
[cpp]
#include <iostream>
using namespace std;
int main()
{
int x =12;
int y =21;
int z = 17;
cout << "x is: " << x <<endl;
cout << "y is: " << y << endl;
cout << "z is: " << z << endl;
swap(x,y,z);
cout << "new x is: " << x << endl;
cout << "new y is: " << y << endl;
cout << "new z is: " << z << endl;
return 0;
}
void swap(int &rX, int &rY, int &rZ)
{
int temp;
temp = rX;
rX = rY;
rY = rZ;
rZ = temp;
}
[/cpp]
BTW, i am using the GCC 4.4.1 compiler if that makes any difference.