Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ functional programming problem

  1. Apr 6, 2012 #1
    As a basic exercise in C++ functional programming, I wrote the following code:

    Code (Text):

    #include <iostream>
    #include <string>
    using namespace std;

    template <class T>
    void Wib (T& a,T& b)
        T temp = a;
        a = b;
        b = temp;

    int main()
        string A = "World!";
        string B = "Hello, ";
        Wib <string> (A,B);
        cout << A << B << endl;
        return 0;
    I get the error error: call of overloaded 'swap(std::string&, std::string&)' is ambiguous when I compile. Why?
    Last edited: Apr 6, 2012
  2. jcsd
  3. Apr 6, 2012 #2
    Never mind. I seem to have tried to overload swap. Replacing all calls to swap with Wibblywobblytimeywimey instead seemed to work. Still have an issue. Why do I need T& a and T& b? I'm struggling a little with pointers. (Original code edited)
    Last edited: Apr 6, 2012
  4. Apr 6, 2012 #3
    Have you considered, ah, I don't know, messing around with the program a little before asking for help on PF? You seem to ask an awful lot of questions about programming on PF, and then add a small note to your posts a few minutes later saying something along the lines of "ah, I got it. Nevermind." That's fine, of course, but you might want to consider my advice nonetheless. :wink:

    In my humble opinion, you shouldn't be messing around with the STL if you're struggling with pointers. Pointers are a very basic and important part of the language, an understanding of which would make it much easier for you to use the STL.

    In any case, Beej's guides are awesome, and he's written a draft for his guide to C programming, which I suspect you can use to learn a little more about pointers (C is pretty much equal to C++ in that respect). Presuming you understand most of the rest of C++, you can start here.
  5. Apr 6, 2012 #4
    Read the bit on pointers, understood it, and googled int&. Thanks, and I also understand everything. :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook