C++ functional programming problem

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
Whovian
Messages
651
Reaction score
3
As a basic exercise in C++ functional programming, I wrote the following code:

Code:
#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:
Physics news on Phys.org
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:
Whovian said:
Never mind. I seem to have tried to overload swap. Replacing all calls to swap with Wibblywobblytimeywimey instead seemed to work.
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:

Whovian said:
Still have an issue. Why do I need T& a and T& b? I'm struggling a little with pointers.
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 http://beej.us/guide/bgc/output/html/multipage/pointers.html.
 
Hobin said:
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 http://beej.us/guide/bgc/output/html/multipage/pointers.html.

Read the bit on pointers, understood it, and googled int&. Thanks, and I also understand everything. :)