C/C++ C++ functional programming problem

AI Thread Summary
The discussion revolves around a C++ code snippet that attempts to swap two strings using a template function. The user encounters a compilation error related to an ambiguous call to the overloaded 'swap' function, which they resolve by renaming their function. They express confusion about the necessity of using references (T& a and T& b) in the function parameters and mention struggling with pointers. Other participants suggest that a solid understanding of pointers is essential for effectively using the Standard Template Library (STL) and recommend resources like Beej's guides for learning about pointers. The user later acknowledges their understanding of pointers and expresses gratitude for the guidance received.
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:
Technology 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. :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
8
Views
2K
Replies
40
Views
3K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
39
Views
4K
Back
Top