Reputation: 13
I'm inexperienced with functors and trying to understand the basics somewhat. All I want is a functor that will take a string and return the same string with all values changed to uppercase. I have the following code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
class myFunctor {
private:
public:
myFunctor(string) {};
string operator() (string);
};
string myFunctor::operator() (string stringToConvert) {
transform(stringToConvert.begin(), stringToConvert.end(), stringToConvert.begin(), toupper);
return stringToConvert;
}
int main() {
myFunctor convert("i want this string converted");
return 0;
}
This obviously results in nothing being printed to the screen, but I was hoping someone could help explain what I must do further to get the desired result here so I can better understand how this system works.
Thanks for the help in advance!
Upvotes: 1
Views: 198
Reputation: 73
Forgive me if I'm wrong, buy by seeing your code I suppose you're not very experienced with c++. I would strongly recommend you reed some tutorials because your making several basic mistakes that can easily be fixed with some reading.
So first of all you do not need to create an object to create a function in c++:
int function_name(int foo){
//do something
};
If you want to use a object you are making several mistakes: First you need to include an identifier after the type:
myFunctor(string string_id) {};
Second it that is a class constructor, and it makes no sens to have it receive an input and then do nothing with it. You either make no constructor and use the default or have the value stored for later use:
string create_input;
myFunctor(string string_id) { create_input = string_id};
Second what you are trying to do is overloading an operator, that is some advanced stuff you should leave for when your a little more experienced.
On the other hand you can declare a normal function, (the name operator
is not valid because it is a keyword use to overload operators) like so:
string myFunctor::function_name() (string stringToConvert) {
transform(stringToConvert.begin(), stringToConvert.end(), stringToConvert.begin(), toupper);
return stringToConvert;
}
and yo would use like:
int main() {
myFunctor convert();
convertor.function_name("i want this string converted");
return 0;
}
Upvotes: 0
Reputation: 12831
That's a good explanation from "Sam Varshavchik"
Here is your modified code for your understanding: I noticed that you did not use std::transform properly. And also do not mix "C" strings and "STD" strings. Very difficult to handle. So removed your header "string.h"
I revisited your code and below is the working version.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class myFunctor {
public:
myFunctor() {};
string operator() (string);
};
string myFunctor::operator() (string stringToConvert) {
//STD transform function
transform(stringToConvert.begin(), stringToConvert.end(), stringToConvert.begin(),[](unsigned char c) { return toupper(c); });
return stringToConvert;
}
int main() {
myFunctor myfun; //You should have no argument constructor
string converted_string = myfun("i want this string converted");//Acttual function object invocation
cout<<converted_string<<endl;
return 0;
}
Upvotes: 0
Reputation: 118435
For starters, a functor is an object that implements an operator()
. It's not an object whose constructor takes a parameter (although special-purpose functors can, that's not the case here). So the first mistake is that your functor has a constructor:
myFunctor(string) {};
And, this constructor doesn't do anything with it's parameter. So that should be the first clue that, in your case, your functor's constructor should take no parameters, and should simply be:
myFunctor() {}
Or, forget the constructor completely, and let the functor class use its default constructor.
Now, with its default constructor, it's time to construct it:
myFunctor convert;
And after that, to invoke the functor:
std::cout << convert("i want this string converted") << std::endl;
Upvotes: 3