Simple transform tolower not working - c++

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 {

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;
}

Related

I don't understand why my stoi says it is not declared when it supposedly is [duplicate]

my question is pretty simple but I can't seem to find it out.
I want to know what libary to include when using stoi. I was using atoi and it works fine with
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
but I get "stoi not declared" when I run with stoi. Thanks
You need to #include <string> and use a compiler that understands C++11. Minimal example:
#include <string>
#include <cassert>
int main()
{
std::string example = "1234";
int i = std::stoi(example);
assert(i == 1234);
return 0;
}
Compile, for example, with g++ -std=c++11.

Invalid Template Argument for vector instantiation

When I run this code I get "invalid template arguments" error on the last line. Please advise. (I've omitted the rest of the code)
#include <iostream>
#include <fstream>
#include<array>
#include <vector>;
using namespace std;
int fileLineCount(string);
int fileExists(string[],int);
int main() {
ifstream archiveFile;
archiveFile.open("StudentRecords.txt");
int lineCount=fileLineCount("StudentRecords.txt");
string line;
vector<string> recordArray;
#include <vector>;
should be
#include <vector>
and of course you need to close the } brace at the end of main() (although probably this was a typo). You should also #include <string>, although some of your headers seem to include it implicitly (probably <iostream>).

Using boost::split brings up a load of weird errors

#include <string>
#include "boost\date_time\gregorian\gregorian.hpp"
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost::gregorian;
using namespace boost;
void printString()
{
vector<string> strs;
boost::split(strs, "string to split", boost::is_any_of(' '));
cout << strs[0];
}
This flags up about 6 errors in Boost and 1 in std. My thinking is the namespaces are messing up. This is an edited version of the actual code base but basically I'm using boost::gregorian for a seperate date_time thing and boost for the algoritm code base. I saw an example and using more than one namespace was fine. For me it's just not letting me use split.
You're passing a single character to boost::is_any_of, but it expects a sequence.
Change the code from:
from: boost::is_any_of(' ')
to: boost::is_any_of(" ") and you should be golden.
(oh yeah, and add #include <vector> and #include <iostream> to your example.

Can't instantiate an istring_iterator using a wistringstream

I'm trying to split a string using the method found in this thread, but I'm trying to adapt it to a wstring. However, I have stumbled upon a weird error. Check the code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(void)
{
wstring str(L"Börk börk");
wistringstream iss(str);
vector<wstring> tokens;
copy(istream_iterator<wstring>(iss), // I GET THE ERROR HERE
istream_iterator<wstring>(),
back_inserter< vector<wstring> >(tokens));
return 0;
}
The exact error message is:
error: no matching function for call to 'std::istream_iterator<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, char, std::char_traits<char>, int>::istream_iterator(std::wistringstream&)'
I think it is saying that it can't instantiate the istream_iterator using the passed iss (which is a wistringstream instead of a istringstream). This is on a Mac using XCode and GCC 4.2. And AFAIK there is no wistring_iterator or anything like that.
It works perfectly when I'm using the non-wstring version. As you might see I have changed the declarations to use wstring, wistringstreams and vector<wstring>, just replaced everything to use the wide version.
What could cause this and why won't it accept the wstring-version?
Per the latter part of the error message, you have to override the default params 2 and 3 on istream_iterator() to match widechar usage elsewhere. In Visual C++ this version compiles OK for me:
copy(istream_iterator<wstring, wchar_t, std::char_traits<wchar_t> >(iss), // I DO NOT GET THE ERROR HERE
istream_iterator<wstring, wchar_t, std::char_traits<wchar_t> >(),
back_inserter< vector<wstring> >(tokens));

String Undeclared In C++

I'm sure this is a really simple thing, but I haven't worked in C++ forever.
14 C:\Dev-Cpp\mainCurl.cpp `string'
undeclared (first use this function)
> #include <stdio.h>
> #include <curl/curl.h>
> #include <string>
> #include <iostream>
>
> int main(void) {
> string url("http://www.google.com"); //
> system("pause");
>
> return 0; }
What am I missing here?
Cheers
You haven't declared your namespace. You need to either declare:
using namespace std;
Or tell the compiler that "string" is in the standard namespace:
std::string url("...");
Or you can announce that you are specifically using std::string and only std::string from std by saying:
using std::string;
Add using namespace std; above the main() definition.
Also, you don't need <stdio.h> if you include <iostream>. Also, in C++ a function that doesn't take arguments doesn't need a "void" argument, simply use parentheses with nothing in between them.
This a so recurring problem...
You missed std:: before string, so it will look like std::string
That's because string belongs to std namespace and if you don't use using directive you must specify where string is.
Alternatively you can use
using namespace std; or more conveniently using std::string before using string class.
You need std::string or using std::string.
try std::string url ("http://www.google.com");
the string class is in std namespace