C++ functional programming problem

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:
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. :)
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
11K
  • · Replies 3 ·
Replies
3
Views
4K