Boost Graph example doesn't compile. - c++

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.

Related

How to instantiate the boost class `boost::posix_time::seconds` C++ class?

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

std::function pointer error: cannot convert &A::a to type std::function<>&&

I'm trying to map string to function pointer, so that I can call the function with iter->second(arg) instead of if-else.
I have written a simple one without class, and it works as expected.
But when I modify it as below, it shows compile errors.
#include <functional>
#include <iostream>
#include <unordered_map>
#include <string>
using std::string;
class A{
private:
int a(int num, string s) { return s.size() + num; }
int b(int num, string s) { return num - s.size(); }
public:
void ido(string str){
typedef std::function<int(int, string)> process_func;
std::unordered_map<string, process_func> m;
m.insert(std::make_pair<string, process_func>("a", &A::a));
// using std::placeholders::_1;
// m.insert(std::make_pair<string, process_func>("a", std::bind(&A::a, this, _1)));
// m["a"] = std::bind(&A::a, this, _1);
// m.insert({{"a", &A::a}, {"b", &A::b}});
auto x = m.find(str);
if(x == m.end()) {
std::cout << "Not supported!" << std::endl;
}
std::cout << x->second(10, "hello") << std::endl;
}
};
int main(int argc, char* argv[]) {
A a;
a.ido(string(argv[1]));
return 0;
}
The errors are:
function.cc: In member function ‘void A::ido(std::string)’:
function.cc:17:65: error: no matching function for call to ‘make_pair(const char [2], int (A::*)(int, std::string))’
m.insert(std::make_pair<string, process_func>("a", &A::a));
^
function.cc:17:65: note: candidate is:
In file included from /usr/include/c++/4.8.2/utility:70:0,
from /usr/include/c++/4.8.2/tuple:38,
from /usr/include/c++/4.8.2/functional:55,
from function.cc: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:
function.cc:17:65: note: cannot convert ‘&A::a’ (type ‘int (A::*)(int, std::string) {aka int (A::*)(int, std::basic_string<char>)}’) to type ‘std::function<int(int, std::basic_string<char>)>&&’
m.insert(std::make_pair<string, process_func>("a", &A::a));
What does the error mean? How to fix it?
While your functions 'a' and 'b' do not depend on 'this' (they do not access anything inside class A), the compiler is not smart enough to deduce this. So the error means that you are trying to convert 'pointer to method' to 'pointer to function', which is incorrect conversion. 'Pointer to method' requires and object to be called on. You need to declare methods 'a' and 'b' as 'static' to indicate that they are actually standalone functions, not methods of the class.

Compilation errors related to template instantiation

The following test program reproduces compilation errors within the context of a larger program:
#include <algorithm>
#include <iostream>
#include <vector>
using std::for_each;
using std::vector;
using std::cout;
using std::endl;
template<typename T, typename C = vector<T>>
class display_container{
public:
display_container(const C& cr):this->cr(cr){this->();}
~display_container(){}
private:
constexpr void operator () (void){if(cr.empty()){cout << "NULL" << " ";} else{for_each(cr.begin(), cr.end(), [](const T& crt){cout << crt << " ";});}}
const C& cr;
};
int main (void){
int n = 5;
vector<int> vec(n, 0);
display_container d(vec);
cout << endl;
return 0;
}
The following is a log of the compiler errors:
g++ -ggdb -std=c++17 -Wall -Werror=pedantic -Wextra -c code.cpp
code.cpp: In constructor ‘display_container<T, C>::display_container(const C&)’:
code.cpp:12:40: error: expected identifier before ‘this’
display_container(const C& cr):this->cr(cr){this->();}
^~~~
code.cpp:12:40: error: expected ‘{’ before ‘this’
code.cpp: In function ‘int main()’:
code.cpp:23:28: error: class template argument deduction failed:
display_container d(vec);
^
code.cpp:23:28: error: no matching function for call to ‘display_container(std::vector<int>&)’
code.cpp:12:9: note: candidate: template<class T, class C> display_container(const C&)-> display_container<T, C>
display_container(const C& cr):this->cr(cr){this->();}
^~~~~~~~~~~~~~~~~
code.cpp:12:9: note: template argument deduction/substitution failed:
code.cpp:23:28: note: couldn't deduce template parameter ‘T’
display_container d(vec);
^
code.cpp:10:7: note: candidate: template<class T, class C> display_container(display_container<T, C>)-> display_container<T, C>
class display_container{
^~~~~~~~~~~~~~~~~
code.cpp:10:7: note: template argument deduction/substitution failed:
code.cpp:23:28: note: ‘std::vector<int>’ is not derived from ‘display_container<T, C>’
display_container d(vec);
^
make: *** [makefile:20: code.o] Error 1
I presume that the remaining errors trickle down from the first error related to the inline constructor definition for the display_container template class.
Any suggestions on what is wrong with the code related to inline constructor definition?
TIA
The compiler can not fetch the template type of vector yet:
#include <algorithm>
#include <iostream>
#include <vector>
using std::for_each;
using std::vector;
using std::cout;
using std::endl;
template<typename T, typename C = vector<T>>
class display_container{
public:
display_container(const C& cr): cr(cr) { (*this)(); }
~display_container(){}
private:
constexpr void operator () (void){if(cr.empty()){cout << "NULL" << " ";} else{for_each(cr.begin(), cr.end(), [](const T& crt){cout << crt << " ";});}}
const C& cr;
};
int main (void){
int n = 5;
vector<int> vec(n, 0);
display_container<int> d(vec);
cout << endl;
return 0;
}
You couldn't (and don't need) qualify data members in member initializer list by this, they're expected to be the identifier. The correct syntax should be
display_container(const C& cr):cr(cr){(*this)();}
You should dereference on this and then call operator() on it (as showed abolve), or you can call operator() explicitly like this->operator()(); (which looks ugly).
You should specify the template argument for display_container.
display_container<int> d(vec);
LIVE

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++11 run templated class function with std::thread

Cannot pass function as argument to std::thread when class is defined as template.
compiler: GCC 4.8.2
language: C++11
code:
//---------test.h-----------------------
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <thread>
using namespace std;
template <class T>
class test
{
public:
test();
void thr(T n);
void testThread();
};
#endif // TEST_H
//---------test.cpp-----------------------
#include "test.h"
template <class T>
test<T>::test()
{
}
template <class T>
void test<T>::thr(T n)
{
cout << n << endl;
}
template <class T>
void test<T>::testThread()
{
T n = 8;
thread t(thr, n);
t.join();
}
//---------main.cpp-----------------------
#include <iostream>
using namespace std;
#include "test.h"
#include "test.cpp"
int main()
{
test<double> tt;
tt.testThread();
return 0;
}
compiler error:
In file included from ../std_threads/main.cpp:5:0:
../std_threads/test.cpp: In instantiation of 'void test<T>::testThread() [with T = double]':
../std_threads/main.cpp:10:19: required from here
../std_threads/test.cpp:19:20: error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, double&)'
thread t(thr, n);
^
../std_threads/test.cpp:19:20: note: candidates are:
In file included from ../std_threads/test.h:4:0,
from ../std_threads/main.cpp:4:
/usr/include/c++/4.8.2/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (test<double>::*)(double); _Args = {double&}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8.2/thread:133:7: note: no known conversion for argument 1 from '<unresolved overloaded function type>' to 'void (test<double>::*&&)(double)'
/usr/include/c++/4.8.2/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8.2/thread:128:5: note: candidate expects 1 argument, 2 provided
/usr/include/c++/4.8.2/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8.2/thread:122:5: note: candidate expects 0 arguments, 2 provided
make: *** [main.o] Error 1
20:48:35: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project std_threads (kit: Desktop)
When executing step 'Make'
You need to fully specify the member function name and pass an argument for the implicit first parameter of the non-static member function:
thread t(&test<T>::thr, this, n);
See std::thread of a member function.
Two problems:
to get a pointer to a member function, you need to use & and qualify the function name with the class name. Member functions names don't convert to pointers in the same way as non-member function names.
member functions need an object to act on.
So in this case, you probably want
thread t(&test<T>::thr, this, n);