Conversion from int* to int& - c++

I've been trying to compile and have played around with the ampersands and still can't figure out what the error is. Any ideas?
qsort.cc:22:23: error: no matching function for call to ‘qsort<int>::quicksort(std::vector<int, std::allocator<int> >*)’
qsort.cc:22:23: note: candidate is:
qsort.h:16:6: note: void qsort<T>::quicksort(std::vector<T>&) [with T = int]
qsort.h:16:6: note: no known conversion for argument 1 from ‘std::vector<int, std::allocator<int> >*’ to ‘std::vector<int, std::allocator<int> >&’
Header:
template <class T>
class qsort
{
public:
void quicksort(vector<T> &v);
void qusort(vector<T> &v, int left, int right);
void print(vector<T> &v);
};
template <class T>
void qsort<T>::quicksort(vector<T> &v)
{
qusort(&v, 0, 0);
}
template <class T>
void qsort<T>::print(vector<T> &v)
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
}
Main:
int main()
{
qsort<int> asort;
vector<int> v;
v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(3);
v.push_back(8);
v.push_back(4);
v.push_back(0);
v.push_back(9);
v.push_back(5);
v.push_back(6);
asort.quicksort(&v);
asort.print(&v);
return 0;
}
Updated errors after ampersand removed out of main function calls (the short version)
qsort.h: In member function ‘void quisort::qusort(std::vector&, int, int) [with T = int]’:
qsort.h:18:5: instantiated from ‘void quisort::quicksort(std::vector&) [with T = int]’
qsort.cc:22:22: instantiated from here
qsort.h:27:38: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
/usr/include/c++/4.6/bits/basic_string.tcc:214:5: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]’ [-fpermissive]
qsort.h:18:5: instantiated from ‘void quisort::quicksort(std::vector&) [with T = int]’
qsort.cc:22:22: instantiated from here
qsort.h:31:9: error: no match for ‘operator<’ in ‘(& v)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator, std::vector<_Tp, _Alloc>::reference = int&, std::vector<_Tp, _Alloc>::size_type = long unsigned int](((long unsigned int)i)) < pivot’
qsort.h:31:9: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)

Your member function takes the arguments by reference. They don't take pointers (that which is returned by the address operator &). You simply need to pass the object and the reference will bind:
asort.quicksort(v);
asort.print(v);

Related

Can I ask why the code for vector push_back depending on the data type doesn't work? [duplicate]

This question already has answers here:
how to do an if else depending type of type in c++ template? [duplicate]
(6 answers)
using std::is_same, why my function still can't work for 2 types
(4 answers)
Closed 1 year ago.
I want to fill a vector with different functions depending on the data type of vector. Now I have three types to consider: int, double, string. If I only consider int and double, the code works perfectly. But if I include string into the template, the code fails. I don't understand why the if statement for string doesn't work. Here is the code:
#include <iostream>
#include <vector>
#include <map>
#include <string>
template <typename T>
void fillV(std::vector<T> &res) {
if (std::is_same<T, double>::value ) res.push_back(3.5);
else if (std::is_same<T, int>::value) res.push_back(2);
else if (std::is_same<T, std::string>::value) res.push_back(std::string("hello"));
}
int main()
{
std::vector<std::string> t;
std::vector<double> t1;
std::vector<int> t2;
fillV<std::string>(t);
fillV<double>(t1);
fillV<int>(t2);
std::cout << t[0] << "\n";
std::cout << t1[0] << "\n";
std::cout << t2[0] << "\n";
}
The error information is
input
stderr
Compilation failed due to following error(s).main.cpp: In instantiation of ‘void fillV(std::vector<T>&) [with T = std::basic_string<char>]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',27)">main.cpp:27:25</span>: required from here
main.cpp:16:42: error: no matching function for call to ‘std::vector >::push_back(double)’
if (std::is_same<T, double>::value ) res.push_back(3.5);
^~~
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::basic_string]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘double’ to ‘const value_type& {aka const std::basic_string&}’
/usr/include/c++/6/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::basic_string]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘double’ to ‘std::vector >::value_type&& {aka std::basic_string&&}’
main.cpp:17:43: error: no matching function for call to ‘std::vector >::push_back(int)’
else if (std::is_same<T, int>::value) res.push_back(2);
^~~
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::basic_string]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘int’ to ‘const value_type& {aka const std::basic_string&}’
/usr/include/c++/6/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::basic_string]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘int’ to ‘std::vector >::value_type&& {aka std::basic_string&&}’
main.cpp: In instantiation of ‘void fillV(std::vector<T>&) [with T = double]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',28)">main.cpp:28:21</span>: required from here
main.cpp:18:51: error: no matching function for call to ‘std::vector::push_back(std::string)’
else if (std::is_same<T, std::string>::value) res.push_back(std::string("hello"));
^~~
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = double; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = double]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘const value_type& {aka const double&}’
/usr/include/c++/6/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = double; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = double]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘std::vector::value_type&& {aka double&&}’
main.cpp: In instantiation of ‘void fillV(std::vector<T>&) [with T = int]’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',29)">main.cpp:29:18</span>: required from here
main.cpp:18:51: error: no matching function for call to ‘std::vector::push_back(std::string)’
else if (std::is_same<T, std::string>::value) res.push_back(std::string("hello"));
^~~
In file included from /usr/include/c++/6/vector:64:0,
from main.cpp:10:
/usr/include/c++/6/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = int]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘const value_type& {aka const int&}’
/usr/include/c++/6/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = int]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘std::vector::value_type&& {aka int&&}’
Updated on 04/14/2021:
Since I can't answer my own question, I will add one more solution other than the overloading, which is by Partial template specialization. It works before C++17. The code is inspired by the first answer, thank you.
template <typename T, int i>
struct test;
template <typename T>
struct test<T, 1> {
void fillV(std::vector<T> &res) {
res.push_back(3.5);
}
};
template <typename T>
struct test<T, 2> {
void fillV(std::vector<T> &res) {
res.push_back(2);
}
};
template <typename T>
struct test<T, 3> {
void fillV(std::vector<T> &res) {
res.push_back("Hello");
}
};
int main()
{
std::vector<std::string> t;
std::vector<double> t1;
std::vector<int32_t> t2;
test<double, 1> a1;
a1.fillV(t1);
test<int32_t, 2> a2;
a2.fillV(t2);
test<std::string, 3> a;
a.fillV(t);
std::cout << t[0] << "\n";
std::cout << t1[0] << "\n";
std::cout << t2[0] << "\n";
}
Updated on 04/17/2021:
Moreover, if overloading is used, the following code would be enough.
void fill_v(std::vector<double>& res) { res.push_back(3.5); }
void fill_v(std::vector<int>& res) { res.push_back(2); }
void fill_v(std::vector<std::string>& res) { res.push_back(std::string("hello")); }
int main()
{
std::vector<std::string> t;
std::vector<double> t1;
std::vector<int32_t> t2;
fillV(t1);
fillV(t2);
fillV(t);
std::cout << t[0] << "\n";
std::cout << t1[0] << "\n";
std::cout << t2[0] << "\n";
}
if performs dispatch at run-time. At compile time, all the if part and else part need to be valid whatever T is.
You can change to Constexpr If (since C++17).
If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded.
If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated.
E.g.
template <typename T>
void fillV(std::vector<T> &res) {
if constexpr (std::is_same<T, double>::value ) res.push_back(3.5);
else if constexpr (std::is_same<T, int>::value) res.push_back(2);
else if constexpr (std::is_same<T, std::string>::value) res.push_back(std::string("hello"));
}
LIVE
Before C++17, you can add some overloads processing different variants of std::vector. E.g.
void fill_v(std::vector<double>& res) { res.push_back(3.5); }
void fill_v(std::vector<int>& res) { res.push_back(2); }
void fill_v(std::vector<std::string>& res) { res.push_back(std::string("hello")); }
template <typename T>
void fillV(std::vector<T> &res) {
if (std::is_same<T, double>::value ) fill_v(res);
else if (std::is_same<T, int>::value) fill_v(res);
else if (std::is_same<T, std::string>::value) fill_v(res);
// or just
// fill_v(res);
}

error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’

I am new in c++. When I run my code got this error :(
Big Sorting.cpp: In function ‘int main(int, const char**)’:
Big Sorting.cpp:13:22: error: no matching function for call to ‘std::vector >::push_back(int&)’
v.push_back(m);
^
In file included from /usr/include/c++/8.1.1/vector:64,
from Big Sorting.cpp:2:
/usr/include/c++/8.1.1/bits/stl_vector.h:1074:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp
= std::__cxx11::basic_string; _Alloc = std::allocator >; std::vector<_Tp,
_Alloc>::value_type = std::__cxx11::basic_string]’
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/8.1.1/bits/stl_vector.h:1074:7: note: no known conversion for argument 1 from ‘int’ to ‘const value_type&’ {aka
‘const std::__cxx11::basic_string&’}
/usr/include/c++/8.1.1/bits/stl_vector.h:1090:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp,
_Alloc>::value_type&&) [with _Tp = std::__cxx11::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type =
std::__cxx11::basic_string]’
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/8.1.1/bits/stl_vector.h:1090:7: note: no known conversion for argument 1 from ‘int’ to
‘std::vector >::value_type&&’ {aka
‘std::__cxx11::basic_string&&’}
here is my code
#include <iostream>
#include <vector>
#include <algorithm>
int main(int argc, char const *argv[]) {
std::vector<std::string> v;
int n, m;
std::cin >> n;
for (size_t i = 0; i < n; i++) {
std::cin >> m;
v.push_back(m);
}
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++){
std::cout << v[i] << '\n';
}
return 0;
}
You are reading int variable m and trying to put it into a vector of strings. You should use std::vector<int> instead.
Bottom line: your code needs only one change, most reasonable one would be to change std::vector<std::string> to std::vector<int>.

No match to std::vector<Foo> when specifying vector size in constructor

class Foo {
vector<Bar> bars;
public:
Foo(int barcount) : { bars(barcount, 0); };
};
I'm trying to turn the bars vector into a vector holding barcount Bars. However I get 2 errors when doing this:
Foo.cpp:8: error: expected identifier before ‘{’ token
Foo(int barcount) : { bars(barcount, 0); };
^
Foo.cpp:8: error: no match for call to ‘(std::vector<Bar>) (int&, int)’
Foo(int barcount) : { bars(barcount, 0); };
Any help on what might be going wrong would be much appreciated.
EDIT: This is my code now (Foo/Bar replaced):
class Machine {
vector<Rotor> rotors;
public:
Machine(int rotorcount) : rotors(rotorcount, 0) {}
};
And I get a pretty long error message:
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>]’:
/usr/include/c++/4.8/bits/stl_vector.h:404:55: required from ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Rotor>]’
Machine.cpp:8:51: required from here
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: error: no matching function for call to ‘std::vector<Rotor>::_M_fill_initialize(std::vector<Rotor>::size_type, int&)’
_M_fill_initialize(static_cast<size_type>(__n), __value);
^
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: note: candidate is:
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = Rotor]
_M_fill_initialize(size_type __n, const value_type& __value)
^
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
The initializer list goes after the comma, but before the constructor body, and is not followed by a semicolon:
Foo(int barcount) : bars(barcount, 0) {};
no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
Sounds like you don't have a constructor that takes int as a single parameter.

Custom STL iterator implementation error

I'm attempting to write, for a proof of concept, an STL like iterator that will iterate over a string by skipping every other character. However, I'm encountering many different strange C++ errors I'm having trouble understanding.
My code is:
#include<iostream>
#include<string>
using std::string;
class TestIterator : public std::iterator<std::forward_iterator_tag, string> {
private:
string::iterator _it;
public:
TestIterator() {}
string& operator++() {
return _it + 2;
}
string& operator=(const string& other) {
_it = other;
}
};
int main(int argc, char** argv) {
string a("123045678");
TestIterator start = a.begin();
TestIterator end = a.end();
string b(start, end);
std::cout << b << std::endl;
return 0;
}
When I compile it I get:
% g++ -std=gnu++0x test.cpp -o test
test.cpp: In member function ‘std::string& TestIterator::operator++()’:
test.cpp:14:16: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >’
return _it + 2;
^
test.cpp: In member function ‘std::string& TestIterator::operator=(const string&)’:
test.cpp:18:9: error: no match for ‘operator=’ (operand types are ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ and ‘const string {aka const std::basic_string<char>}’)
_it = other;
^
test.cpp:18:9: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test.cpp:1:
/usr/include/c++/4.8/bits/stl_iterator.h:708:11: note: __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >& __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >::operator=(const __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >&)
class __normal_iterator
^
/usr/include/c++/4.8/bits/stl_iterator.h:708:11: note: no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘const __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >&’
/usr/include/c++/4.8/bits/stl_iterator.h:708:11: note: __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >& __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >::operator=(__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >&&)
/usr/include/c++/4.8/bits/stl_iterator.h:708:11: note: no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >&&’
test.cpp: In function ‘int main(int, char**)’:
test.cpp:24:32: error: conversion from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to non-scalar type ‘TestIterator’ requested
TestIterator start = a.begin();
^
test.cpp:25:28: error: conversion from ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to non-scalar type ‘TestIterator’ requested
TestIterator end = a.end();
^
In file included from /usr/include/c++/4.8/string:53:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from test.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc: In instantiation of ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = TestIterator; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’:
/usr/include/c++/4.8/bits/basic_string.h:1725:56: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = TestIterator; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/4.8/bits/basic_string.h:1746:58: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = TestIterator; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/4.8/bits/basic_string.tcc:229:49: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = TestIterator; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
test.cpp:26:22: required from here
/usr/include/c++/4.8/bits/basic_string.tcc:128:12: error: no match for ‘operator==’ (operand types are ‘TestIterator’ and ‘TestIterator’)
if (__beg == __end && __a == _Alloc())
^
/usr/include/c++/4.8/bits/basic_string.tcc:128:12: note: candidates are:
...
Is there any other way to do something like this (that is, provide a new iterator over the string class) without storing iterators inside my class that will act as an iterator? I've piece together this class from many different code listings online, so the semantics may not be perfect.
Any help on resolving this iterator issue would be much appreciated.
STL iterators are created by container objects, not constructed from the containers themselves:
int main(int argc, char** argv) {
string a("abcdefghijk");
TestIterator start = a.begin(); //<-----------
TestIterator stop = a.end(); //<-----------
string b(start, stop); //<-----------
std::cout << b << std::endl;
return 0;
}
Therefore, your new iterators must be able to be constructed from string::iterator which string::begin() returns.
STL iterators do not have begin() and end() themselves; that's the job of the container.
operator++() should return a reference to the iterator being incremented, not a reference to the container it points to.

typename in dependent scope

Below is a condensed version of my code that gives me a compiler error. The compiler tells me to put typename in front of 'std::deque::reverse_iterator', which makes sense. But if I do I receive the error at the bottom. What does it mean? How can it be resolved?
#include <iostream>
#include <deque>
template<class T>
class Stack{
public:
Stack(){}
~Stack(){}
void push(T c) { s.push_back(c); }
void inspect() const{
for(typename std::deque<T>::reverse_iterator i=s.rbegin(); i!=s.rend(); i++)
std::cout << *i << std::endl;
}
private:
typename std::deque<T> s;
};
int main(){
Stack<int> s;
s.push(1);
s.inspect();
return 0;
}
Error:
error: no matching function for call to 'std::_Deque_iterator<int, int&, int*>::_Deque_iterator(std::reverse_iterator<std::_Deque_iterator<int, const int&, const int*> >::iterator_type)'|
note: candidates are:|
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator(const iterator&) [with _Tp = int; _Ref = int&; _Ptr = int*; std::_Deque_iterator<_Tp, _Ref, _Ptr>::iterator = std::_Deque_iterator<int, int&, int*>]|
note: no known conversion for argument 1 from 'std::reverse_iterator<std::_Deque_iterator<int, const int&, const int*> >::iterator_type {aka std::_Deque_iterator<int, const int&, const int*>}' to 'const iterator& {aka const std::_Deque_iterator<int, int&, int*>&}'|
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator() [with _Tp = int; _Ref = int&; _Ptr = int*]|
note: candidate expects 0 arguments, 1 provided|
note: std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Deque_iterator(_Tp*, std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Map_pointer) [with _Tp = int; _Ref = int&; _Ptr = int*; std::_Deque_iterator<_Tp, _Ref, _Ptr>::_Map_pointer = int**]|
note: candidate expects 2 arguments, 1 provided|
There's nothing dependent about std::deque<T>, so there mustn't be a typename. Only things to the right of a :: where the left depends on a template parameter is dependent.
This is a good example of where using auto would help you more than just saving typing. You're in a const member function, but trying to use your data member's reverse_iterator, not const_reverse_iterator.
Change typename std::deque<T>::reverse_iterator to typename std::deque<T>::const_reverse_iterator, or, more simply, auto.
This is in addition to the extra typename on your data member.