Method with const parameter does not accept non-const parameter? - c++

This code does not compile:
ErrorTolerantSearch e;
e.readStringsFromFile("test.txt");
e.buildQgramIndex(3);
vector<map<uint, uint>*> lists;
lists.push_back(&e._qgramIndex["ret"]); // ignore this, assume container not empty
lists.push_back(&e._qgramIndex["coo"]); // ignore this, assume container not empty
map<uint, uint> resunion = e.computeUnion(lists); // <-- this makes problems
This is a part of the header
class ErrorTolerantSearch {
public:
void readStringsFromFile(string fileName);
void buildQgramIndex(uint q);
map<uint, uint> computeUnion(const vector<const map<uint, uint>*> & lists);
map<string, map<uint, uint> > _qgramIndex;
};
This is the error the compiler gives:
ErrorTolerantSearchTest.cpp: In member function ‘virtual void ErrorTolerantSearchTest_computeUnion_Test::TestBody()’:
ErrorTolerantSearchTest.cpp:89:50: error: no matching function for call to ‘ErrorTolerantSearch::computeUnion(std::vector<std::map<unsigned int, unsigned int>*>&)’
ErrorTolerantSearchTest.cpp:89:50: note: candidate is:
In file included from ErrorTolerantSearchTest.cpp:36:0:
./ErrorTolerantSearch.h:56:19: note: std::map<unsigned int, unsigned int> ErrorTolerantSearch::computeUnion(const std::vector<const std::map<unsigned int, unsigned int>*>&)
./ErrorTolerantSearch.h:56:19: note: no known conversion for argument 1 from ‘std::vector<std::map<unsigned int, unsigned int>*>’ to ‘const std::vector<const std::map<unsigned int, unsigned int>*>&’
make[1]: *** [ErrorTolerantSearchTest] Fehler 1
But what is the problem? I dont get it. I never had problems with passing non-const variables to functions with const parameters by reference.

std::vector<const T> is not equal to std::vector<T> and no convertible to it.

Related

Issue regarding inheritance - "type is not a base class"

I'm trying to create a functional inheritance hierarchy. We are working with implementing our own priority queue class template, and the program includes lists such as buy orders.
My idea was to have "p_queue" as the base class, and let the "orders" class inherit from it, and then let subclasses such as "buy_orders" inherit from the orders class. My reasoning is that every order in this program is a priority queue, as they will be sorted by it.
Normally I would understand the error "type 'base_class' is not a direct base of 'sub_class'" if the derived class wasn't directly inheriting the superclass. But since the orders class inherits the p_queue class and no other class directly inherits from priority queue class, I'm confused why I get this error message.
Here is the code
//main.cpp
#include "orders.h"
int main(){
return 0;
}
//orders.h
#include "p_queue.h"
template<typename T>
struct less{
bool operator()(const T& a, const T& b){
return a < b;
}
};
template<typename T>
class Orders : public p_queue<T, decltype(less<T>())> {
private:
size_t m_Size;
std::vector<T> list;
public:
Orders(const size_t& sz, const std::vector<T>& l) : list(l), m_Size(sz),
p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){}
virtual const size_t getSize() const { return m_Size; }
virtual const std::vector<T> getList() const = 0;
};
struct buy_orders : public Orders<size_t>{
std::vector<size_t> buy_prices;
buy_orders(const size_t& sz) : Orders(sz, buy_prices) {}
};
//p_queue.h
#include <functional>
template<typename T, typename Compare = std::less<T>>
class p_queue{
protected:
T* m_first;
T* m_last;
Compare m_comp;
public:
p_queue(T* first, T* last, Compare comp = Compare()) :
m_first(first), m_last(last), m_comp(comp) {}
};
The above code produces the error:
<source>: In instantiation of 'Orders<T>::Orders(const size_t&, const std::vector<T>&) [with T = long unsigned int; size_t = long unsigned int]':
<source>:39:57: required from here
<source>:31:59: error: type 'p_queue<long unsigned int, std::less<long unsigned int> >' is not a direct base of 'Orders<long unsigned int>'
31 | p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){}
| ^
<source>:31:59: error: no matching function for call to 'p_queue<long unsigned int, less<long unsigned int> >::p_queue()'
<source>:13:5: note: candidate: 'p_queue<T, Compare>::p_queue(T*, T*, Compare) [with T = long unsigned int; Compare = less<long unsigned int>]'
13 | p_queue(T* first, T* last, Compare comp = Compare()) :
| ^~~~~~~
<source>:13:5: note: candidate expects 3 arguments, 0 provided
<source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(const p_queue<long unsigned int, less<long unsigned int> >&)'
7 | class p_queue{
| ^~~~~~~
<source>:7:7: note: candidate expects 1 argument, 0 provided
<source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(p_queue<long unsigned int, less<long unsigned int> >&&)'
<source>:7:7: note: candidate expects 1 argument, 0 provided
ASM generation compiler returned: 1
<source>: In instantiation of 'Orders<T>::Orders(const size_t&, const std::vector<T>&) [with T = long unsigned int; size_t = long unsigned int]':
<source>:39:57: required from here
<source>:31:59: error: type 'p_queue<long unsigned int, std::less<long unsigned int> >' is not a direct base of 'Orders<long unsigned int>'
31 | p_queue<T>(&(*list.begin()), &(*list.end()), less<T>()){}
| ^
<source>:31:59: error: no matching function for call to 'p_queue<long unsigned int, less<long unsigned int> >::p_queue()'
<source>:13:5: note: candidate: 'p_queue<T, Compare>::p_queue(T*, T*, Compare) [with T = long unsigned int; Compare = less<long unsigned int>]'
13 | p_queue(T* first, T* last, Compare comp = Compare()) :
| ^~~~~~~
<source>:13:5: note: candidate expects 3 arguments, 0 provided
<source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(const p_queue<long unsigned int, less<long unsigned int> >&)'
7 | class p_queue{
| ^~~~~~~
<source>:7:7: note: candidate expects 1 argument, 0 provided
<source>:7:7: note: candidate: 'constexpr p_queue<long unsigned int, less<long unsigned int> >::p_queue(p_queue<long unsigned int, less<long unsigned int> >&&)'
<source>:7:7: note: candidate expects 1 argument, 0 provided
If I remove the buy_orders struct from orders.h, the problem disappears. Why is this? How can I solve this issue?
You actually have 2 problems here.
First one is simply that you have to pass explicitely the second template parameter to the p_queue constructor:
...
Orders(const size_t& sz, const std::vector<T>& l) : list(l), m_Size(sz),
p_queue<T, decltype(less<T>())>(&(*list.begin()), &(*list.end()), less<T>()) {}
...
is enough to make the type 'base_class' is not a direct base of 'sub_class' error disappear.
But there is still another problem, even if it only raises a warning. Base classes are initialized before data members in C++, no matter the order of initializers. That means that p_queue constructor will be called before initialization of list, leading to Undefined Behaviour, because it explicitely uses the first and last members before they get a chance to be initialized. That means that you have to set up a 2 phases initialization of p_queue, with a default initialization first, and then set its m_first and m_last members in Orders constructor body:
...
p_queue(Compare comp = Compare()) : m_comp(comp) {}
p_queue(T* first, T* last, Compare comp = Compare()) :
m_first(first), m_last(last), m_comp(comp) {}
...
and
...
Orders(const size_t& sz, const std::vector<T>& l) : list(l), m_Size(sz),
p_queue<T, decltype(less<T>())>(less<T>()) {
this->m_first = &(*list.begin());
this->m_last = &(*list.end());
}
...
Problem 1
The problem is that p_queue<T> and p_queue<T, decltype(::less<T>())> are two different class-type and you're inheriting from the latter(p_queue<T, decltype(::less<T>())>) but trying to use the constructor of the former(p_queue<T>) in the member initializer list.
To solve this just make sure that you use the constructor of p_queue<T, decltype(::less<T>())> instead of p_queue<T> in the member initializer list as shown below:
template<typename T>
class Orders : public p_queue<T, decltype(::less<T>())> {
private:
size_t m_Size;
std::vector<T> list;
public:
Orders(const size_t& sz, const std::vector<T>& l) : list(l), m_Size(sz),
//--------------vvvvvvvvvvvvvvvvvvvv---->added this second template argument
p_queue<T , decltype(::less<T>())>(&(*list.begin()), &(*list.end()), ::less<T>()){}
virtual const size_t getSize() const { return m_Size; }
virtual const std::vector<T> getList() const = 0;
};
Working demo
Problem 2
Additionally as base class ctor will be used before the derived class member like list is initialized, and since you're using *list.begin() as an argument to the base class ctor, the program has undefined behavior.
Clang gives a warning about this:
<source>:36:43: warning: field 'list' is uninitialized when used here [-Wuninitialized]
p_queue<T , decltype(::less<T>())>(&(*list.begin()), &(*list.end()), ::less<T>()){}
^
<source>:44:36: note: in instantiation of member function 'Orders<unsigned long>::Orders' requested here
buy_orders(const size_t& sz) : Orders(sz, buy_prices) {}
^
<source>:36:61: warning: field 'list' is uninitialized when used here [-Wuninitialized]
p_queue<T , decltype(::less<T>())>(&(*list.begin()), &(*list.end()), ::less<T>()){}

Creating an unindent algorithm using boost::range

I'm creating an unindent algorithm for a text-editor. I've managed to obtain the range to operate on, but when I want to do the Gtk::TextBuffer::erase, it fails:
void unindentSelection(const Glib::RefPtr<Gtk::TextBuffer> &buffer)
{
Gtk::TextBuffer::iterator start, end;
buffer->get_selection_bounds(start, end);
auto selRange = boost::make_iterator_range(start, end);
auto buffRange = boost::make_iterator_range(buffer->begin(), buffer->end());
auto prevRangeRev = boost::make_iterator_range(buffRange.begin(), selRange.begin()) | boost::adaptors::reversed;
auto prevRangeLineRev = boost::find<boost::return_begin_found>(prevRangeRev, '\n');
auto prevRangeLine = prevRangeLineRev | boost::adaptors::reversed;
auto afterRange = boost::make_iterator_range(selRange.end(), buffRange.end());
auto afterRangeLine = boost::find<boost::return_begin_found>(afterRange, '\n');
auto exSelRangeAux = boost::join(prevRangeLine, selRange);
auto exSelRange = boost::join(exSelRangeAux, afterRangeLine);
show_range(exSelRange);
while (true)
{
auto spaceRange = boost::find_if<boost::return_begin_found>(exSelRange, findNonspaceNL);
if (boost::distance(spaceRange))
{
buffer->erase(spaceRange.begin(), spaceRange.end());
}
}
}
TextEditor.cpp:501:31: error: no viable conversion from 'boost::range_detail::join_iterator >, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>' to 'const iterator' (aka 'const Gtk::TextIter')
buffer->erase(spaceRange.begin(), afterRangeLine.end());
^~~~~~~~~~~~~~~~~~
/usr/include/gtkmm-3.0/gtkmm/textiter.h:145:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'boost::range_detail::join_iterator >, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>' to 'const Gtk::TextIter &' for 1st argument
class TextIter
^
/usr/include/gtkmm-3.0/gtkmm/textiter.h:145:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'boost::range_detail::join_iterator >, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>, Gtk::TextIter, unsigned int, unsigned int, boost::iterators::bidirectional_traversal_tag>' to 'Gtk::TextIter &&' for 1st argument
class TextIter
^
/usr/include/gtkmm-3.0/gtkmm/textbuffer.h:378:34: note: passing argument to parameter 'range_begin' here
iterator erase(const iterator& range_begin, const iterator& range_end);
Any ideas?
I had to create an iterator type that contained a templated 'Iterator it' inside (which in my case is the Gtk::TextIter), using 'boost::iterator_facade'. All operations to the iterator where forwarded to 'it', except the operator*(), that just gave the plain iterator. So the erasing was done in this way:
buffer->erase(*spaceRange.begin(), *spaceRange.end());
This new type of iterator will be very useful manipulating ranges. Not sure this is the best solution, but works.

Iterator over a list of pairs in C++?

I defined a list of pairs and wish to access them using an iterator, following an example.
class A{
private:
list<pair<size_type,size_type> > l_edge_;
public:
void function() const{
list<pair<size_type,size_type> >::iterator Iter_;
Iter_ = l_edge_.begin();
}
}
However, I got a compilation error. How can I fix it?
error: no match for 'operator=' (operand types are 'const iterator
{aka const std::_List_iterator<std::pair<unsigned int, unsigned int> >}' and
'std::list<std::pair<unsigned int, unsigned int> >::const_iterator
{aka std::_List_const_iterator<std::pair<unsigned int, unsigned int> >}')
My guess is that you are trying to write a const member function, and not what you copied in the question:
void function() const
{
Iter_ = l_edge_.begin();
}
Now, since the function is const, the l_edge_ member is also const, and so, begin() returns a const_iterator instead of a plain iterator. But that hardly matters because the Iter_ member is also const, so it cannot be assigned to.
Usually you do not want to declare iterators as member variables, unless very special needs. Instead, just declare a local one when you need it, and of the appropriate constness:
class A
{
private:
list<pair<size_type,size_type> > l_edge_;
public:
//const member function
void function() const
{
list< pair<size_type,size_type> >::const_iterator iter = l_edge_.begin();
}
//non-const member function
void function()
{
list< pair<size_type,size_type> >::iterator iter = l_edge_.begin();
}
};

Is it possible to alias boost::make_iterator_range?

I am having trouble aliasing the function boost::make_iterator_range
(I would like to hide boost behind an alias in case this particular library gets adopted into the standard sometime in the future.)
Is there any way this can be made to work?
#include <boost/range/iterator_range.hpp>
void Foo()
{
}
template< typename T >
void Bar()
{ }
template< typename T >
void Bar(char c)
{ }
void (&FooAlias)() = Foo; // ok
void (&BarAlias)() = Bar<int>; // ok
// boost::iterator_range<const size_t*> (&MakeIterRangeAlias)(const size_t*,const size_t*) =
// boost::make_iterator_range<const size_t*>; // not ok
int main(int argc, char** argv)
{
const size_t v[] = { 3, 5, 1, 5, 29, 15 };
boost::iterator_range<const size_t*> r
= boost::make_iterator_range( std::begin( v ), std::end( v )); // want to alias this
return 0;
}
The error message is:
In file included from /usr/include/boost/iterator/iterator_categories.hpp:15:0,
from /usr/include/boost/iterator/detail/facade_iterator_category.hpp:7,
from /usr/include/boost/iterator/iterator_facade.hpp:14,
from /usr/include/boost/range/iterator_range_core.hpp:23,
from /usr/include/boost/range/iterator_range.hpp:13,
from sandbox.cpp:2:
/usr/include/boost/mpl/eval_if.hpp: In instantiation of ‘boost::mpl::eval_if_c<true, boost::range_const_iterator<const long unsigned int*>, boost::range_mutable_iterator<const long unsigned int* const> >’:
/usr/include/boost/range/iterator.hpp:63:63: instantiated from ‘boost::range_iterator<const long unsigned int* const>’
sandbox.cpp:20:10: instantiated from here
/usr/include/boost/mpl/eval_if.hpp:60:31: error: no type named ‘type’ in ‘boost::mpl::eval_if_c<true, boost::range_const_iterator<const long unsigned int*>, boost::range_mutable_iterator<const long unsigned int* const> >::f_ {aka struct boost::range_const_iterator<const long unsigned int*>}’
/usr/include/boost/mpl/eval_if.hpp: In instantiation of ‘boost::mpl::eval_if_c<false, boost::range_const_iterator<const long unsigned int*>, boost::range_mutable_iterator<const long unsigned int*> >’:
/usr/include/boost/range/iterator.hpp:63:63: instantiated from ‘boost::range_iterator<const long unsigned int*>’
sandbox.cpp:20:10: instantiated from here
/usr/include/boost/mpl/eval_if.hpp:60:31: error: no type named ‘type’ in ‘boost::mpl::eval_if_c<false, boost::range_const_iterator<const long unsigned int*>, boost::range_mutable_iterator<const long unsigned int*> >::f_ {aka struct boost::range_mutable_iterator<const long unsigned int*>}’
sandbox.cpp:20:10: error: invalid initialization of non-const reference of type ‘void (&)(const size_t*, const size_t*) {aka void (&)(const long unsigned int*, const long unsigned int*)}’ from an rvalue of type ‘<unresolved overloaded function type>’
make: *** [sandbox] Error 1
Using function pointers is a suboptimal way alias a function. It is not as flexible as the original (it can no longer be a template) and you now need to know the exact signature of the function, which may or may not be stable.
Instead try this approach.
template< typename ... Args >
auto MakeIterRangeAlias( Args&& ... args ) -> decltype( /* copy return line here */ )
{
return boost::make_iterator_range( std::forward<Args>(args)... );
}
With almost no work on your part the alias supports the exact signature of the original. Even if it dramatically changes, you're still set. Further, unlike the function pointer approach the optimizer will be able to trivially inline MakeIterRangeAlias so that there is no runtime overhead.

size_t and unsigned int does not match in a template function's parameter list

I want to use a stack to store indices of an array,so I use the following typedef,where istack is a template class for stack:
typedef istack<size_t> IndexStack;
and I declare a stack by
IndexStack stack;
But when I call the following function (where A.size() returns a size_t);
stack.push_back(A.size());
GCC gives the following error
sort.cpp: In function 'void quicksort2(Array&)':
sort.cpp:50:27: error: no matching function for call to 'istack<unsigned int>::push_back(size_t)'
iarray.h:103:8: note: candidate is: void istack<T>::push_back(T&) [with T = unsigned int]
How can I make it work?
#include <cstddef>
template <class T>
struct istack
{
void push_back(T& value);
std::size_t size() const;
};
int main()
{
typedef istack<size_t> IndexStack;
IndexStack a, stack;
stack.push_back(a.size());
}
This code produces an error
In function 'int main()':
13 no matching function for call to 'istack<unsigned int>::push_back(size_t)'
note 5 candidates are: void istack<T>::push_back(T&) [with T = unsigned int]
Note that it lists candidates. (I suspect you are not reading / posting the entire error message.)
The given candidate doesn't match the call, because the reference is non-const. A temporary (such as the result of a.size()) cannot be bound to a non-const reference.
push_back should be taking a const T& value