Using boost::split brings up a load of weird errors - c++

#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.

Related

C++ program gives a run-time error when strings are used

#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
string st = "Hello world";
return 0;
}
and
#include <string>
int main ()
{
std::string st = "Hello world";
return 0;
}
I tried compiling this code using minGW compiler on netbeans. It brings up the following error after the successful build.
RUN FAILED (exit value -1,073,741,511, total time: 93ms)
But it works clean when strings are not used. I would like to know what I am doing wrong here. Thanks in advance.
Use c++ strings and don't use using namespace std:
#include <string> //c++ string header
int main ()
{
std::string st = "Hello world";
return 0;
}
#include <string.h> is the old C-style string header and most likely isn't what you want to use here. See this question for more details: Difference between <string> and <string.h>?
Note: If you really wanted the old C-style strings then you really should be using #include <cstring> because this will put those functions into the std namespace and won't cause any namespace pollution that can lead to other undesirable outcomes.
Likely what happened was that you used the old style string header and didn't properly initialize those strings. The old C-style strings don't have a constructor and operator= defined like the std::string class.
Edit: After looking at the Netbeans forum this is a problem with Netbeans and not a c++ issue. Try changing the output to an external terminal in Netbeans. Or run the program directly from the command line. If these approaches don't fix the problem or are undesirable then make a post over on the Netbeans forum. Also have a look at this question: Program won't run in NetBeans, but runs on the command line!
Uss #include <string> instead of string.h

Simple transform tolower not working

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

why is string not declared in scope

I have the following code:
#include <string>
#include <boost/thread/tss.hpp>
static boost::thread_specific_ptr<string> _tssThreadNameSptr;
I get the following error
g++ -c -I$BOOST_PATH tssNaming.h
tssNaming.h:7: error: 'string' was not declared in this scope
But I am including string in my #include.
You have to use std::string since it's in the std namespace.
string is in the std namespace. You have the following options:
Write using namespace std; after the include and enable all the std names: then you can write only string on your program.
Write using std::string after the include to enable std::string: then you can write only string on your program.
Use std::string instead of string
I find that including:
using namespace std;
To your C++ code saves a lot of time in debugging especially in situations like yours where std:: string is required and also it will help in keeping your code clean.
With this in mind, your code should be:
#include <string>
using namespace std;
#include <boost/thread/tss.hpp>
static boost::thread_specific_ptr<string> _tssThreadNameSptr;

std has no member 'getline'?

I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>

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