C++ functional programming problem

Click For Summary

Discussion Overview

The discussion revolves around a C++ functional programming exercise involving a template function intended to swap two string variables. Participants explore issues related to function overloading, the use of references, and pointers in C++.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant encounters a compilation error related to the ambiguous call of overloaded 'swap' when trying to use a custom swap function.
  • Another participant suggests that the error may stem from inadvertently overloading the standard swap function and proposes renaming the function to avoid conflicts.
  • There is a question about the necessity of using references (T& a and T& b) in the function definition, indicating a struggle with understanding pointers.
  • A participant advises against using the Standard Template Library (STL) without a solid understanding of pointers, emphasizing their importance in C++.
  • Resources are shared, including a guide on pointers, which one participant finds helpful in clarifying their understanding.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of using STL without a strong grasp of pointers, and there is no consensus on the best approach to resolving the initial compilation error.

Contextual Notes

Some participants express uncertainty about the use of references and pointers, indicating a potential gap in foundational knowledge that may affect their understanding of the STL.

Who May Find This Useful

Individuals learning C++ programming, particularly those struggling with pointers and the STL, may find this discussion relevant.

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

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · 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
10K
  • · Replies 3 ·
Replies
3
Views
4K