I want to split by comma, and I have the following class which is instantiated with a comma-separated line. The class is as follows:
#include <sstream>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <vector>
#include <string>
#include <set>
#include <vector>
#include <boost/tokenizer.hpp>
class Packet {
public:
int packetEndDateTime;
int creationTimeStamp;
std::string mydatetime;
std::string micses;
std::string message_type;
std::string teid;
std::string teid_cp;
std::string teid_data;
std::string apn;
std::string msisdn;
std::string cause;
std::string causeText;
std::string responseDate;
std::string allData;
std::string fields[9];
int fieldPos = 0;
/*
boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens);
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
*/
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens); // empty token policy
Packet(){ }
Packet(std::string inMessage){
set_message(inMessage);
}
void set_message(std::string inMessage){
allData = inMessage;
tokenizer tokens(inMessage, sep);
for ( tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter ){
fields[fieldPos] = *tok_iter;
fieldPos++;
}
mydatetime = fields[0];
message_type = fields[1];
teid = fields[2];
teid_cp = fields[3];
teid_data = fields[4];
cause = fields[5];
apn = fields[6];
msisdn = fields[7];
}
};
The compiler is coming back with:
g++ -o ggsnGiParser welcome.cc -lboost_filesystem -lboost_program_options -lboost_system -std=c++11
In file included from welcome.cc:49:0:
Packet.hpp:39:41: error: expected identifier before ','
Packet.hpp:39:41: error: expected ‘,’ or ‘...’ before ','
Packet.hpp: In member function ‘void Packet::set_message(std::string)’:
Packet.hpp:51:40: error: no matching function for call to ‘boost::tokenizer<boost::char_separator<char> >::tokenizer(std::string&, <unresolved overloaded function type>)’
Packet.hpp:51:40: note: candidates are:
In file included from Packet.hpp:12:0,
from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:62:5: note: template<class Container> boost::tokenizer::tokenizer(const Container&, const TokenizerFunc&)
/usr/include/boost/tokenizer.hpp:62:5: note: template argument deduction/substitution failed:
In file included from welcome.cc:49:0:
Packet.hpp:51:40: note: cannot convert ‘((Packet*)this)->Packet::sep’ (type ‘<unresolved overloaded function type>’) to type ‘const boost::char_separator<char>&’
In file included from Packet.hpp:12:0,
from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:58:5: note: template<class Container> boost::tokenizer::tokenizer(const Container&)
/usr/include/boost/tokenizer.hpp:58:5: note: template argument deduction/substitution failed:
In file included from welcome.cc:49:0:
Packet.hpp:51:40: note: candidate expects 1 argument, 2 provided
In file included from Packet.hpp:12:0,
from welcome.cc:49:
/usr/include/boost/tokenizer.hpp:53:5: note: boost::tokenizer<TokenizerFunc, Iterator, Type>::tokenizer(Iterator, Iterator, const TokenizerFunc&) [with TokenizerFunc = boost::char_separator<char>; Iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; Type = std::basic_string<char>]
/usr/include/boost/tokenizer.hpp:53:5: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’
/usr/include/boost/tokenizer.hpp:32:9: note: boost::tokenizer<boost::char_separator<char> >::tokenizer(const boost::tokenizer<boost::char_separator<char> >&)
/usr/include/boost/tokenizer.hpp:32:9: note: candidate expects 1 argument, 2 provided
/usr/include/boost/tokenizer.hpp:32:9: note: boost::tokenizer<boost::char_separator<char> >::tokenizer(boost::tokenizer<boost::char_separator<char> >&&)
/usr/include/boost/tokenizer.hpp:32:9: note: candidate expects 1 argument, 2 provided
And I really don't understand where the problem might be...
Any help is greatly appreciated!
David
Replace
boost::char_separator<char> sep(",", "|", boost::keep_empty_tokens); // empty token policy
with
boost::char_separator<char> sep = {",", "|", boost::keep_empty_tokens}; // empty token policy
When constructing within a class declaration, you have to avoid that particular () syntax.
There may be more errors hidden by this.
Related
I found the following class definition in the boost library 1.71.0 which is installed at /usr/include/boost in my case.
class BOOST_SYMBOL_VISIBLE seconds : public time_duration
{
public:
template <typename T>
explicit seconds(T const& s,
typename boost::enable_if<boost::is_integral<T>, void>::type* = BOOST_DATE_TIME_NULLPTR) :
time_duration(0,0, numeric_cast<sec_type>(s))
{}
};
The Above class definition can be found out at /usr/include/boost/date_time/posix_time/posix_time_duration.hpp
I am using following code-snippet where the class boost::posix_time::seconds is instantiated:
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/format.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/shared_array.hpp>
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <vector>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
int main()
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::deadline_timer timer(io_service);
timer.expires_from_now(boost::posix_time::seconds(60.0))
return 0;
}
Let's say I save the above code-snippet with the file name boost.cpp. Then I compile boost.cpp using the command
g++ -I /usr/include/boost -pthread boost.cpp
However, I am getting the following error:
boost.cpp: In function ‘int main()’:
boost.cpp:19:59: error: no matching function for call to ‘boost::posix_time::seconds::seconds(double)’
19 | timer.expires_from_now(boost::posix_time::seconds(60.0))
| ^
In file included from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:16,
from /usr/include/boost/asio/time_traits.hpp:23,
from /usr/include/boost/asio/detail/timer_queue_ptime.hpp:22,
from /usr/include/boost/asio/detail/deadline_timer_service.hpp:29,
from /usr/include/boost/asio/basic_deadline_timer.hpp:24,
from /usr/include/boost/asio.hpp:25,
from boost.cpp:1:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note: candidate: ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*)’
57 | explicit seconds(T const& s,
| ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: note: template argument deduction/substitution failed:
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp: In substitution of ‘template<class T> boost::posix_time::seconds::seconds(const T&, typename boost::enable_if<boost::is_integral<T>, void>::type*) [with T = double]’:
boost.cpp:19:59: required from here
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:57:16: error: no type named ‘type’ in ‘struct boost::enable_if<boost::is_integral<double>, void>’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(const boost::posix_time::seconds&)’
53 | class BOOST_SYMBOL_VISIBLE seconds : public time_duration
| ^~~~~~~
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: no known conversion for argument 1 from ‘double’ to ‘const boost::posix_time::seconds&’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: candidate: ‘boost::posix_time::seconds::seconds(boost::posix_time::seconds&&)’
/usr/include/boost/date_time/posix_time/posix_time_duration.hpp:53:30: note: no known conversion for argument 1 from ‘double’ to ‘boost::posix_time::seconds&&’
What I can think of is the use of the line boost::posix_time::seconds(60.0) is not correct but I am not able to figure out what's the correct way to instantiate the posix_time::seconds class based on the above definitions. Do any of you have an idea about it?
That constructor takes an integral (whole) number. boost::posix_time::seconds(60) should work.
Reference
I have the following function (for testing):
static bool foo(void)
{
std::string name = "name";
std::vector<std::string> test;
std::vector<std::string>::iterator vStart = test.begin();
std::vector<std::string>::iterator vEnd = test.end();
return (std::find(vStart, vEnd, name) == vEnd);
}
And I get a compilation error:
/data/src/fiware-orion/src/lib/common/string.cpp: In function 'bool foo()':
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: error: no matching function for call to 'find(std::vector<std::basic_string<char> >::iterator&, std::vector<std::basic_string<char> >::iterator&, std::string&)'
return (std::find(vStart, vEnd, name) == vEnd);
^
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: note: candidate is:
In file included from /usr/include/c++/4.9/bits/locale_facets.h:48:0,
from /usr/include/c++/4.9/bits/basic_ios.h:37,
from /usr/include/c++/4.9/ios:44,
from /usr/include/c++/4.9/istream:38,
from /usr/include/c++/4.9/sstream:38,
from /data/src/fiware-orion/src/lib/common/string.cpp:31:
/usr/include/c++/4.9/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)
find(istreambuf_iterator<_CharT> __first,
^
/usr/include/c++/4.9/bits/streambuf_iterator.h:369:5: note: template argument deduction/substitution failed:
/data/src/fiware-orion/src/lib/common/string.cpp:167:39: note: '__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >' is not derived from 'std::istreambuf_iterator<_CharT>'
return (std::find(vStart, vEnd, name) == vEnd);
Maybe the message which points to the problem is this:
template argument deduction/substitution failed:
but as far as I undersand the concrete classes used in the find() function argument (std::vector<std::string>::iterator, std::vector<std::string>::iterator and std::string) are clear.
What's specially surprises me is that this same code fragment for foo() function is working verbatim in other parts of my code (i.e. other .cpp files) so maybe it is related somehow with the #include chain in a way I'm not able to deduce or trace...
Any help is welcome!
There is no find from #include <algorithm> in the error message, only the one from streambuf_iterator.h. Add #include <algorithm>.
You are returning an iterator, but your function declaration is 'void'
I think you forgot to include <algorithm>
Please add this #include <algorithm>
Consider the following code:
#include <string>
#include <map>
#include <memory>
#include <utility>
#include <iostream>
typedef std::shared_ptr<const std::string> ConstDataTypePtr;
typedef std::map<std::string, ConstDataTypePtr> StrDataTypeMap;
int main()
{
StrDataTypeMap m_nameToType;
ConstDataTypePtr vp_int8(new std::string("RGH"));
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
return 0;
}
You must compile it with: g++ -std=c++11 <filename>.cpp.
It gives the following error:
testO.cpp: In function ‘int main()’:
testO.cpp:14:88: error: no matching function for call to ‘make_pair(const char [7], ConstDataTypePtr&)’
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
^
testO.cpp:14:88: note: candidate is:
In file included from /usr/include/c++/4.8.2/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8.2/bits/char_traits.h:39,
from /usr/include/c++/4.8.2/string:40,
from testO.cpp:1:
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
make_pair(_T1&& __x, _T2&& __y)
^
/usr/include/c++/4.8.2/bits/stl_pair.h:276:5: note: template argument deduction/substitution failed:
testO.cpp:14:88: note: cannot convert ‘vp_int8’ (type ‘ConstDataTypePtr {aka std::shared_ptr<const std::basic_string<char> >}’) to type ‘std::shared_ptr<const std::basic_string<char> >&&’
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>("int8_t", vp_int8));
From what I am reading of the error, the compiler is expecting an r-value when I am trying to insert into the map. Why? What mistake have I made here?
Kindly note that I created this snippet from some existing code which is part of a large code-base. It is probably also worth mentioning that the snippet has been taken from a code base which was run on Windows and I have the task of porting it to Linux. The original author had used std::tr1::shared_ptr. I modified it to use std::shared_ptr. I didn't expect any repercussions because of this change.
The whole point of std::make_pair is to let compiler deduce types. If you want to provide type, use std::pair<K, V>
So
m_nameToType.insert(std::make_pair<std::string, std::string>("int8_t", vp_int8));
should be:
m_nameToType.insert(std::make_pair("int8_t", vp_int8));
or
m_nameToType.insert(std::pair<const std::string, ConstDataTypePtr>("int8_t", vp_int8));
or simply:
m_nameToType.emplace("int8_t", vp_int8);
#include <memory>
#include <map>
#include <string>
int main() {
using shared_data = std::shared_ptr<const std::string>;
std::map<std::string, shared_data> map;
map.insert(std::make_pair(
"something",
shared_data(new std::string("something else"))
));
return 0;
}
see: http://ideone.com/4AQfqd
Back to your problem;
testO.cpp:14:83: note: cannot convert ‘vp_int8’ (type ‘ConstDataTypePtr {aka std::shared_ptr >}’) to type ‘std::basic_string&&’
m_nameToType.insert(std::make_pair("int8_t", vp_int8));
What you have:
std::make_pair<std::string, std::string>(some_string, TOTALLY_NOT_A_STRING)
You gave wrong types to the std::make_pair template. Just change
m_nameToType.insert(std::make_pair<std::string, std::string>("int8_t", vp_int8));
Into
m_nameToType.insert(std::make_pair<std::string, ConstDataTypePtr>(std::string("int8_t"), vp_int8));
(note the std::make_pair<std::string, ConstDataTypePtr> part)
EDIT: or don't provide template params at all, as someone suggested in comment.
Don't mention the types in the template in make_pair function.
m_nameToType.insert(std::make_pair("int8_t", vp_int8));
I'm trying to convert a vector<int> to a vector<string>. Using std::transform I used std::to_string to convert the int to string but I keep getting an error. Here's my code
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
int main(){
std::vector<int> v_int;
std::vector<std::string> v_str;
for(int i = 0;i<5;++i)
v_int.push_back(i);
v_str.resize(v_int.size());
std::transform(v_int.begin(),v_int.end(),v_str.begin(),std::to_string);
}
but I'm getting this error
no matching function for call to 'transform'
std::transform(v_int.begin(),v_int.end(),v_str.begin(),std::to_string);
^~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1951:1: note:
candidate template ignored: couldn't infer template argument
'_UnaryOperation'
transform(_InputIterator __first, _InputIterator __last, _OutputIterato...
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1961:1: note:
candidate function template not viable: requires 5 arguments, but 4 were
provided
transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputItera...
std::to_string is an overloaded function, so you'll need to provide a cast to disambiguate
std::transform(v_int.begin(),v_int.end(),v_str.begin(),
static_cast<std::string(*)(int)>(std::to_string));
Or use a lambda
std::transform(v_int.begin(),v_int.end(),v_str.begin(),
[](int i){ return std::to_string(i); });
I have a vector in my Header file, and I'm trying to do a bool function that returns the find() function, but it is giving me an error.
vector<string> reservedWord{
....
....
....
};
bool function
bool isReservedWord(string str)
{
return find(reservedWord.begin(), reservedWord.end(), str) != reservedWord.end();
}
I tried it both without the last != reservedWord.end) and also without.
The errors given are these:
||=== Build: Release in compilers (compiler: GNU GCC Compiler) ===|
E:\University\compilers\reservedWords.h||In function 'bool isReservedWord(std::string)':|
E:\University\compilers\reservedWords.h|40|error: no matching function for call to 'find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&)'|
E:\University\compilers\reservedWords.h|40|note: candidate is:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\streambuf_iterator.h|371|note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&)|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\streambuf_iterator.h|371|note: template argument deduction/substitution failed:|
E:\University\compilers\reservedWords.h|40|note: '__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >' is not derived from 'std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >'|
E:\University\compilers\reservedWords.h|41|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Here's a working example. Look at how your code is different. Ask questions as required. :-)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
std::vector<std::string> g_reserved
{
"the",
"quick",
"brown",
"fox",
"jumps",
"over",
"the",
"lazy",
"dog"
};
bool IsReserved(const std::string &s)
{
return g_reserved.end() !=
std::find(g_reserved.cbegin(), g_reserved.cend(), s);
}
int main()
{
std::cout << std::boolalpha
<< IsReserved("fox")
<< ' '
<< IsReserved("zebra")
<< std::endl;
return 0;
}
I got the same compilation error when <string> was included but <algorithm> was not. In that case the compiler only sees the declaration of
std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)
and quits with this error.
To fix this, add
#include <algorithm>