Sign in with
Sign up | Sign in
Your question
Solved

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

Last response: in Applications
Share
a b L Programming
November 1, 2009 5:18:33 PM

I just started using C++, and im currently working on using references to do multiple returns.

This sample code simply swaps two ints:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int x =12;
  7. int y =21;
  8.  
  9. cout << "x is: " << x <<endl;
  10. cout << "y is: " << y << endl;
  11.  
  12. swap(x,y);
  13.  
  14. cout << "new x is: " << x << endl;
  15. cout << "new y is: " << y << endl;
  16.  
  17. return 0;
  18. }
  19.  
  20. void swap(int &rX, int &rY)
  21. {
  22. int temp;
  23.  
  24. temp = rX;
  25. rX = rY;
  26. rY = temp;
  27. }


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?

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int x =12;
  7. int y =21;
  8. int z = 17;
  9.  
  10. cout << "x is: " << x <<endl;
  11. cout << "y is: " << y << endl;
  12. cout << "z is: " << z << endl;
  13.  
  14. swap(x,y,z);
  15.  
  16. cout << "new x is: " << x << endl;
  17. cout << "new y is: " << y << endl;
  18. cout << "new z is: " << z << endl;
  19.  
  20. return 0;
  21. }
  22.  
  23. void swap(int &rX, int &rY, int &rZ)
  24. {
  25. int temp;
  26.  
  27. temp = rX;
  28. rX = rY;
  29. rY = rZ;
  30. rZ = temp;
  31. }


BTW, i am using the GCC 4.4.1 compiler if that makes any difference.
a b L Programming
November 2, 2009 2:24:23 AM

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
m
0
l
Related resources
a b L Programming
November 3, 2009 11:18:10 AM

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?
m
0
l
a b L Programming
November 3, 2009 3:15:00 PM

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
m
0
l

Best solution

November 3, 2009 11:59:30 PM

you need a function prototype
  1. void swap(int &, int &, int &);


before main and it is generally accepted to have this after the include and using statements
Share
a b L Programming
November 4, 2009 12:02:12 AM

mindless728 said:
you need a function prototype
  1. void swap(int &, int &, int &);


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

I vaguely remember something about that from my basic C++ classes years ago.
m
0
l
a b L Programming
November 5, 2009 5:31:36 AM

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.
m
0
l
November 5, 2009 11:40:40 AM

my guess, there is a function prototype for it in one of the header files used for iostream, though you would have to look closely at what gcc includes
m
0
l
a b L Programming
November 9, 2009 2:05:06 PM

As mindless728 said, one way is to declare a function prototype, but you can also just declare the function before using it (put swap() before main()).
m
0
l
a b L Programming
November 9, 2009 9:57:59 PM

Zenthar said:
As mindless728 said, one way is to declare a function prototype, but you can also just declare the function before using it (put swap() before main()).

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.
m
0
l
November 10, 2009 3:41:59 AM

randomizer said:
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.


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
m
0
l
a b L Programming
November 10, 2009 3:55:48 AM

They don't teach you that in basic C++. This was very basic high school stuff.
m
0
l
November 11, 2009 2:06:07 AM

well, i never took c++ in high school, i learned asm for the z80 and m68k, not until college did i get into c++ work
m
0
l
a b L Programming
November 11, 2009 2:12:56 AM

Well if you know asm, you can consider yourself 1337 right? :D  You can now write programs to run precisely how you want them, it just might take you a bit longer ;) 
m
0
l
November 11, 2009 2:22:52 PM

well for my major you would have to know asm (at least one)
m
0
l
!