C++ functional programming problem

In summary, the conversation discusses a code written for a basic exercise in C++ functional programming. The code initially caused an error due to an attempt to overload the swap function, but was later resolved by replacing the calls to swap with a different function. The conversation also touches on the importance of understanding pointers in programming and suggests resources for learning more about them. The conversation concludes with the individual expressing their understanding and gratitude.
  • #1
Whovian
652
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
  • #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:
  • #3
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.
 
  • #4
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. :)
 
  • #5


The error is occurring because there are multiple functions with the name "swap" in the standard library, and the compiler is unable to determine which one to use. This is a common issue in C++ when using templates, as the compiler needs to match the correct type for the template parameters. In this case, there are multiple functions with the name "swap" that could potentially match the template parameters <string>, such as std::swap or std::string::swap. To fix this error, you can specify which function to use by using the namespace or by explicitly calling the function with its full name. For example, using std::swap(A, B) or using namespace std; before calling Wib.
 

Related to C++ functional programming problem

1. What is functional programming in C++?

Functional programming in C++ is a programming paradigm that focuses on writing code in a declarative style, where functions are treated as first-class objects and can be passed as arguments or returned as values. This allows for writing concise and reusable code that is easier to reason about and debug.

2. How is functional programming different from traditional programming in C++?

In traditional programming, the focus is on writing code in an imperative style, where the emphasis is on how to perform a task rather than what task to perform. In functional programming, the focus is on what task to perform, and the code is written in a more declarative and modular manner.

3. What are the benefits of using functional programming in C++?

Some of the benefits of using functional programming in C++ include improved code readability, increased code reuse, easier parallelization, and better handling of complex data structures. It also helps in writing more efficient and bug-free code.

4. Are there any limitations to using functional programming in C++?

One limitation of using functional programming in C++ is that it may not be suitable for all types of problems. Functional programming also has a steeper learning curve compared to traditional programming, and it may not be the best approach for writing performance-critical applications.

5. How can I get started with functional programming in C++?

To get started with functional programming in C++, you can start by learning the basic concepts of functional programming, such as higher-order functions, immutability, and recursion. You can also explore libraries like STL and Boost, which offer functional programming capabilities. Practice writing small programs and gradually incorporate functional programming into your projects.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top