find() not working in C++ Header - c++

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>

Related

no matching function call error using std::find

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>

Passing std algorithm. Template deducing [duplicate]

This question already has answers here:
Why can't "transform(s.begin(),s.end(),s.begin(),tolower)" be complied successfully?
(5 answers)
Closed 6 years ago.
I was trying to make this small example work
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<char> v{'a', 'b', 'c', 'd'};
std::transform(v.begin(), v.end(), v.begin(), std::toupper);
std::for_each(v.begin(), v.end(), [](char & c) {
std::cout << c << " ";
});
}
I think it's obvious, what i'm trying to achive. However, I get these errors
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp||In function 'int main()':|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|error: no matching function for call to 'transform(std::vector<char>::iterator, std::vector<char>::iterator, std::vector<char>::iterator, <unresolved overloaded function type>)'|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note: candidates are:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4152|note: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4152|note: template argument deduction/substitution failed:|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note: couldn't deduce template parameter '_UnaryOperation'|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4189|note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\stl_algo.h|4189|note: template argument deduction/substitution failed:|
C:\Users\vitaly.bushaev\Documents\vk cup\1.cpp|8|note: candidate expects 5 arguments, 4 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
So I'm reading this and I understand the error, but i can't understand why is it happening. Sure this works fine:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<char> v{'a', 'b', 'c', 'd'};
using it = std::vector<char>::iterator;
std::transform<it, it, int (int)>(v.begin(), v.end(), v.begin(), std::toupper);
std::for_each(v.begin(), v.end(), [](char & c) {
std::cout << c << " ";
});
}
So here's the question:
1) Why c++ can't deduce template parameter here ?
2) Is there a way around that, to not specify types ?
There is more than one overload for std::toupper.
Template deduction happens before overload selection, so you have to either be specific or wrap the call in a lambda or function object.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<char> v{'a', 'b', 'c', 'd'};
// method 1
struct make_upper
{
int operator()(int ch) const { return std::toupper(ch); }
};
std::transform(v.begin(), v.end(), v.begin(), make_upper());
// method 2
std::transform(v.begin(), v.end(), v.begin(), [](auto&& ch) { return std::toupper(ch); });
}
references:
http://en.cppreference.com/w/cpp/locale/toupper
http://en.cppreference.com/w/cpp/string/byte/toupper

C++ tokenizer separator not compiling

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.

Boost Graph example doesn't compile.

I took the code from
http://www.boost.org/doc/libs/1_54_0/libs/graph/doc/edge_list.html
included my header,
#include <iostream> // for std::cout
#include <utility> // for std::pair
#include <algorithm> // for std::for_each
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/edge_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include <boost/graph/johnson_all_pairs_shortest.hpp>
#include <fstream>
#include <ctime>
using namespace boost;
int main(){
enum { u, v, x, y, z, N };
char name[] = { 'u', 'v', 'x', 'y', 'z' };
typedef std::pair<int,int> E;
E edges[] = { E(u,y), E(u,x), E(u,v),
E(v,u),
E(x,y), E(x,v),
E(y,v), E(y,z),
E(z,u), E(z,x) };
int weight[] = { -4, 8, 5,
-2,
9, -3,
7, 2,
6, 7 };
typedef boost::edge_list<E*> Graph;
Graph g(edges, edges + sizeof(edges) / sizeof(E));
std::vector<int> distance(N, std::numeric_limits<short>::max());
std::vector<int> parent(N,-1);
distance[z] = 0;
parent[z] = z;
bool r = boost::bellman_ford_shortest_paths(g, int(N), weight,
distance.begin(),
parent.begin());
if (r)
for (int i = 0; i < N; ++i)
std::cout << name[i] << ": " << distance[i]
<< " " << name[parent[i]] << std::endl;
else
std::cout << "negative cycle" << std::endl;
return 0;
}
compile with
g++ -O3 boostexampl.cpp -I/user/include/
I got this error
make -k
g++ -O3 boostexampl.cpp -I/user/include/
boostexampl.cpp: In function ‘int main()’:
boostexampl.cpp:40:61: error: no matching function for call to ‘bellman_ford_shortest_paths(Graph&, int, int [10], std::vector<int>::iterator, std::vector<int>::iterator)’
boostexampl.cpp:40:61: note: candidates are:
In file included from boostexampl.cpp:7:0:
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:91:8: note: template<class EdgeListGraph, class Size, class WeightMap, class PredecessorMap, class DistanceMap, class BinaryFunction, class BinaryPredicate, class BellmanFordVisitor> bool boost::bellman_ford_shortest_paths(EdgeListGraph&, Size, WeightMap, PredecessorMap, DistanceMap, BinaryFunction, BinaryPredicate, BellmanFordVisitor)
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:91:8: note: template argument deduction/substitution failed:
boostexampl.cpp:40:61: note: candidate expects 8 arguments, 5 provided
In file included from boostexampl.cpp:7:0:
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:210:8: note: template<class EdgeListGraph, class Size, class P, class T, class R> bool boost::bellman_ford_shortest_paths(EdgeListGraph&, Size, const boost::bgl_named_params<P, T, R>&)
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:210:8: note: template argument deduction/substitution failed:
boostexampl.cpp:40:61: note: mismatched types ‘const boost::bgl_named_params<P, T, R>’ and ‘int [10]’
In file included from boostexampl.cpp:7:0:
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:222:8: note: template<class EdgeListGraph, class Size> bool boost::bellman_ford_shortest_paths(EdgeListGraph&, Size)
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:222:8: note: template argument deduction/substitution failed:
boostexampl.cpp:40:61: note: candidate expects 2 arguments, 5 provided
In file included from boostexampl.cpp:7:0:
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:229:8: note: template<class VertexAndEdgeListGraph, class P, class T, class R> bool boost::bellman_ford_shortest_paths(VertexAndEdgeListGraph&, const boost::bgl_named_params<T, Tag, Base>&)
/usr/local/include/boost/graph/bellman_ford_shortest_paths.hpp:229:8: note: template argument deduction/substitution failed:
boostexampl.cpp:40:61: note: mismatched types ‘const boost::bgl_named_params<T, Tag, Base>’ and ‘int’
make: *** [examp] Error 1
make: Target `main' not remade because of errors.
Compilation exited abnormally with code 2 at Sat Oct 5 14:24:35
I am kind of stuck here. Any help would be appreciated. Sorry I provides the full code, but I don't know where is problem is. Is boost examples guaranteed to work? did they change the interface but didn't change on-line example? Or I didn't include headers.

no matching function for call to ‘find'

I have the following code:
#include <algorithm>
#include <vector>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
typedef vector<int> IntContainer;
typedef IntContainer::iterator IntIterator;
IntContainer vw;
IntIterator i = find(vw.begin(), vw.end(), 5);
if (i != vw.end())
{
printf("Find 5 in vector\n"); // found it
}
else
{
printf("Couldn't find 5 in vector\n"); // couldn't found it
}
return 0;
}
I try to compile it on Ubuntu with gcc 4.7.1 and get the following error:
vec_test.cpp: In function ‘int main()’:
vec_test.cpp:27:46: error: no matching function for call to ‘find(std::vector<int>::iterator, std::vector<int>::iterator, int)’
vec_test.cpp:27:46: note: candidate is:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/locale_facets.h:50:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/basic_ios.h:39,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ios:45,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/iostream:40,
from vec_test.cpp:3:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: 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&)
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: note: template argument deduction/substitution failed:
vec_test.cpp:27:46: note: ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ is not derived from ‘std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >’
This code doesn't do anything since the vector is not initialized with any content but it should compile.
I suspect this is a gcc problem but after lots of digging I'm quit desperate.
Please let me know if anyone encountered this problem and knows how to solve it.
Maybe some file defines a function find in global namespace?
Have you tried specifying full-scope? That's to say, std::find instead of find. It could help deleting that weird line:
using namespace std;
However, it is not what I would expect. I would call it a bug.