trouble with transform

This is a discussion on trouble with transform within the C++ Programming forums, part of the General Programming Boards category; I'm having difficulty understanding these error messages and why my code doesn't work Code: /Purpose: Demonstrates pass by refrence using ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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);

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    http://faq.cprogramming.com/cgi-bin/...wer=1046053421

    Needs a cast. Funny little thingy.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    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)

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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);
    }
    Last edited by Ancient Dragon; 06-11-2006 at 02:44 PM.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for the tip with -> and pointers for some reason I have always had the habbit of casting (*pointer).memfunc()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d Cubik program in C
    By joot43 in forum C Programming
    Replies: 4
    Last Post: 05-25-2009, 08:17 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. transform help!!!
    By what3v3r in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 10:27 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21