-
trouble with transform
I'm having difficulty understanding these error messages and why my code doesn't work
Code:
/Purpose: Demonstrates pass by refrence using pointers and refrences
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
void name_to_upper(string* tempName){
transform ((*tempName).begin(), (*tempName).end(), (*tempName).begin(), toupper);
}
void name_to_lower(string& tempName){
transform (tempName.begin(), tempName.end(), tempName.begin(),tolower);
}
int main(int argc, char *argv[])
{
string name;
cout << "Enter your name then I will display it in all caps, then in all lower letters: ";
cin >> name;
cout << "Original Name -> " << name << endl;
name_to_upper(&name);
cout << "In caps ->" << name << endl;
name_to_lower(name);
cout << "All lower -> "<< name << endl;
return EXIT_SUCCESS;
}
Error Messages:
/home/ripspinner/passbyrefrence/src/passbyrefrence.cpp: In function ‘void name_to_upper(std::string*)’:
/home/ripspinner/passbyrefrence/src/passbyrefrence.cpp:34: error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)’
/home/ripspinner/passbyrefrence/src/passbyrefrence.cpp: In function ‘void name_to_lower(std::string&)’:
/home/ripspinner/passbyrefrence/src/passbyrefrence.cpp:38: error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::
Please help :confused:
-
why did you make those pointers? use references and it will simplify the code. Also some compilers have problems with tuppper and tolower -- try the :: operator.
Code:
void name_to_upper(string& tempName){
transform (tempName.begin(), tempName.end(),tempName.begin(), ::toupper);
}
int main(int argc, char *argv[])
{
string name;
...
name_to_upper(name);
-
-
It's just a learning exercise to show that pass by refrence can use pointers or refrences.
Anyhow your solution seems to have worked, (I haven't run it yet though).
Thanks!
BTW no cast was necessary (thats what I suspected at first too but nope)
-
well if you really want to pass by pointer, then use its -> operator. There is no point passing as a pointer then casting back to reference.
Code:
void name_to_upper(string* tempName){
transform (tempName->begin(), tempName->end(),tempName->begin(), ::toupper);
}
-
It would need a cast if you were using std::toupper, which I guess is what you unintentionally did with your using namespace std; dealio.
-
Thanks for the tip with -> and pointers for some reason I have always had the habbit of casting (*pointer).memfunc()