Solved

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

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.
15 answers Last reply Best Answer
More about function references working advice
  1. maybe this can help you

    http://msdn.microsoft.com/en-us/library/s5120k8y%28VS.71%29.aspx

    How i see, you don't load the <string> library
  2. 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
  3. 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?
  4. 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
  5. Best answer
    you need a function prototype
    
    void swap(int &, int &, int &);
    


    before main and it is generally accepted to have this after the include and using statements
  6. mindless728 said:
    you need a function prototype
    
    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.
  7. 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.
  8. 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
  9. 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()).
  10. 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.
  11. 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
  12. They don't teach you that in basic C++. This was very basic high school stuff.
  13. 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
  14. 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 ;)
  15. well for my major you would have to know asm (at least one)
Ask a new question

Read More

Programming Apps