C++, function using references not working. Advice?

kyeana

Distinguished
May 21, 2008
230
0
18,860
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.
 
Solution
you need a function prototype
Code:
void swap(int &, int &, int &);

before main and it is generally accepted to have this after the include and using statements

kyeana

Distinguished
May 21, 2008
230
0
18,860
No offense, but did you even look at my code? I am using integers, not strings, therefore there is no reason to #include <string>.

The first code that i posted (with only 2 references being passed in) works, and the integers are swapped. However the second code (with 3 references being passed in) gives me an error at compile time.

Any other advice as to why this might not be working? Could it be a limitation of C++? a compiler issue? just bad coding on my part?

Thanks
 

randomizer

Distinguished
If it is anything like C# I don't think there's any (practical) limit to the number of references you can send, however my knowledge of C++ is pretty poor. It's a rather odd error, I can't see anything glaringly wrong with the function call or signature. It's possible that because there is a "built-in" swap function (or appears to be based on the article linked above) there might be issues with overloading it. Have you tried calling the function something else?
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
When i initally wrote this code it had a different name and didn't work, however ill try it again when i get home.

My first thought is that the name shouldn't be a problem for two reasons. First, the swap method shown above is part of <string>, and sense i haven't included that it seems like there would be nothing there to overload, and secondly the first swap function works while the second one doesn't.

Ill try it again with a different name, and if that doesn't work ill try a different compiler and post back with how it fares
 

randomizer

Distinguished

I vaguely remember something about that from my basic C++ classes years ago.
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
Done and done. Thanks for the info.

Do you know why it worked with the 2 references but failed with the 3 references without the prototype? Not that it really matters, i will make sure to include these in the prototype in the cpp or header file from now on, i am just curious.

Thanks for the help.
 

randomizer

Distinguished

Bah, now I remember one of the first things I learned back in C++ classes. Put main() at the bottom! :lol: I've been working with C# now mostly, and positioning doesn't make a difference, so those kinds of "rules" quickly get forgotten.
 

mindless728

Distinguished
Jul 15, 2008
130
0
18,660


or you can put the prototypes on top and then the definitions on the bottom
or put them in a header file and include that