0

So I've seen a lot of similar issues but none of the answers are fixing my issue. Can someone explain why this code:

string LinkedListByName::toLower(string stringToConvert){

return std::transform(stringToConvert.begin(), stringToConvert.end(), stringToConvert.begin(), ::tolower); }

is giving me this error:

conversion from `__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to non-scalar type `std::string' requested

In the project I'm going to need to convert a lot of strings to lower and boost is NOT an option. I literally copied and pasted this code from previous projects in which it functioned.

Additionally the header file is including the following:

#include <vector>
using namespace std;
#include <iostream>
using namespace std;
#include <string>
using namespace std;
#include <algorithm>
#include "Node.h"
namespace model {
Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
Noob
  • 145
  • 2
  • 9
  • 3
    `using namespace std;` is a bad idea at nearly all times, but this is certainly the first time I've seen it used three times in a row... – us2012 Feb 24 '13 at 02:07

1 Answers1

3

Your method should return string, but instead you try to return iterator from std::transform. Change it to this:

string LinkedListByName::toLower(string stringToConvert){
    std::transform(stringToConvert.begin(), stringToConvert.end(), stringToConvert.begin(), ::tolower); 
    return stringToConvert;
}
Slava
  • 43,454
  • 1
  • 47
  • 90
  • Wow. For some reason I got hung up on the idea that it returned a string, but it doesn't it only acts on it. either way this worked so thanks. – Noob Feb 24 '13 at 02:10
  • How many times do you need to copy the `string`? Perhaps it would be better to pass it by reference not return anything. – johnsyweb Feb 24 '13 at 02:26
  • "Better" is very subjective term and I doubt we can decide what is better for Nood. Especially when we have so small amount of information. – Slava Feb 24 '13 at 03:14
  • @Slava: Hence why I qualified it. – johnsyweb Feb 25 '13 at 00:14
  • It may not be better in many situations. For example in current form it can be used as unary transformation function and can be applied on certain algorithms, where variant with reference cannot be, or will be less efficient. – Slava Feb 25 '13 at 00:27