A question from a C++ newbie. Forgive me if its a simple one.
I am trying to run a *.cpp code that reads a text file and outputs a formatted file. However when running the code, I am getting some compilation errors. I have attached herewith the code and errors.
The code:
#include<iostream>
#include<iomanip>
#include<fstream>
const int cell = 1;
using namespace std;
int main()
{
int id[cell],crop[cell],n;
double root_depth1[cell], root_depth2[cell], root_depth3[cell], root_fract1[cell], root_fract2[cell], root_fract3[cell], x;
double LAI_1[cell],LAI_2[cell],LAI_3[cell],LAI_4[cell],LAI_5[cell],LAI_6[cell];
double LAI_7[cell], LAI_8[cell], LAI_9[cell], LAI_10[cell], LAI_11[cell], LAI_12[cell];
ifstream read;
read.open("veg_par_in.txt");
if(read != 0)
{
for (int i=0;i<cell;i++)
{
read>>id[i]>>crop[i]>>root_depth1[i]>>root_fract1[cell]>>root_depth2[cell]>>root_fract2[cell]>>root_depth3[cell]>>root_fract3[cell];
read>>LAI_1[i]>>LAI_2[i]>>LAI_3[i]>>LAI_4[i]>>LAI_5[i]>>LAI_6[i];
read>>LAI_7[i]>>LAI_8[i]>>LAI_9[i]>>LAI_10[i]>>LAI_11[i]>>LAI_12[i];
}
read.close();
ofstream write;
write.open("veg_par.txt");
for(int j=0;j<cell;j++)
{
write<<id[j]<<" "<<1<<endl;
write<<right<<setw(5)<<crop[j]<<" "<<fixed<<setprecision(6)<<float(1)<<fixed<<setprecision(2)<<" "<<root_depth1[j]<<" "<<root_fract1[cell];
write<<" "<<root_depth2[cell]<<" "<<root_fract2[cell]<<" "<<root_depth3[cell]<<" "<<root_fract3[cell]<<endl;
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j];
write<<" "<<LAI_7[j]<<" "<<LAI_8[j]<<" "<<LAI_9[j]<<" "<<LAI_10[j]<<" "<<LAI_11[j]<<" "<<LAI_12[j]<<endl;
}
write.close();
}
else
cout<<"veg_par_in.txt file is missing.";
}
The error message:
veg_par.cpp: In function ‘int main()’:
veg_par.cpp:17:10: error: no match for ‘operator!=’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘int’)
if(read != 0)
veg_par.cpp:17:10: note: candidate: operator!=(int, int) <built-in>
veg_par.cpp:17:10: note: no known conversion for argument 1 from ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘in ’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iosfwd:40:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::
operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/postypes.h:221:5: note: template argument deduction/substitution fa iled:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::fpos<_StateT>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:64:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_pair.h:448:5: note: candidate: template<class _T1, class _T2> con stexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_pair.h:448:5: note: template argument deduction/substitution fa iled:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::pair<_T1, _T2>’
if(read != 0)In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:304:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:304:5: note: template argument deduction/substitutio n failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::reverse_iterato r<_Iterator>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:361:5: note: candidate: template<class _IteratorL, cla ss _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:361:5: note: template argument deduction/substitutio n failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::reverse_iterato r<_Iterator>’
if(read != 0)
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1125:5: note: candidate: template<class _IteratorL, cl ass _IteratorR> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1125:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::move_iterator<_ IteratorL>’
if(read != 0)
^In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1131:5: note: candidate: template<class _Iterator> boo l std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator!=(const move_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/stl_iterator.h:1131:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::move_iterator<_ IteratorL>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:158:5: note: candidate: template<class _T1, class _T2> bo ol std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:158:5: note: template argument deduction/substitution f ailed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::allocator<_Char T>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:164:5: note: candidate: template<class _Tp> bool std::ope rator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/allocator.h:164:5: note: template argument deduction/substitution f ailed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::allocator<_Char T>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5112:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_Ch arT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5112:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::basic_string<_C harT, _Traits, _Alloc>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5125:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5125:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: mismatched types ‘const _CharT*’ and ‘std::basic_ifstream<char>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5137:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_string.h:5137:5: note: template argument deduction/substituti on failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::basic_string<_C harT, _Traits, _Alloc>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/ios_base.h:46:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:311:3: note: candidate: bool std::operator!=(const std::error _code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:311:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:315:3: note: candidate: bool std::operator!=(const std::error _code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:315:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:319:3: note: candidate: bool std::operator!=(const std::error _condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:319:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_condition&’
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:323:3: note: candidate: bool std::operator!=(const std::error _condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/system_error:323:3: note: no known conversion for argument 1 from ‘std:: ifstream {aka std::basic_ifstream<char>}’ to ‘const std::error_condition&’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_facets.h:48:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/basic_ios.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ios:44,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iostream:39,
from veg_par.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, c lass _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT , _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/streambuf_iterator.h:210:5: note: template argument deduction/subst itution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::istreambuf_iter ator<_CharT, _Traits>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:39:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/array:246:5: note: candidate: template<class _Tp, long unsigned int _Nm> b ool std::operator!=(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/array:246:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::array<_Tp, _Nm ’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:37:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:1367:5: note: candidate: template<class ... _TElements, class ... _U Elements> constexpr bool std::operator!=(const std::tuple<_Elements ...>&, const std::tuple<_Elements ...>&)
operator!=(const tuple<_TElements...>& __t,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/tuple:1367:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::tuple<_Elements ...>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:667:5: note: candidate: template<class _Tp, class _Dp, c lass _Up, class _Ep> bool std::operator!=(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, _Ep>&)
operator!=(const unique_ptr<_Tp, _Dp>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:667:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:673:5: note: candidate: template<class _Tp, class _Dp> b ool std::operator!=(const std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)
operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:673:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: ‘std::ifstream {aka std::basic_ifstream<char>}’ is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
if(read != 0)
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/locale_conv.h:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/locale:43,
from /usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/iomanip:43,
from veg_par.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:678:5: note: candidate: template<class _Tp, class _Dp> b ool std::operator!=(std::nullptr_t, const std::unique_ptr<_Tp, _Dp>&)
operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include/c++/bits/unique_ptr.h:678:5: note: template argument deduction/substitution failed:
veg_par.cpp:17:13: note: mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘int’
if(read != 0)
^
veg_par.cpp:34:109: error: expected primary-expression before ‘,’ token
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j ];
^
veg_par.cpp:34:113: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator<<’
write<<" "<<fixed<<setprecision(3)<<LAI_1[j]<<" "<<LAI_2[j]<< " "<<LAI_3[j]<<" "<<LAI_4[j]<<" "<<LAI_5[j]<," "<<LAI_6[j ];
~~~^~~~~~~~~ ~
The first error says there is no match for 'operator!=' with operand types char and int. I have been looking into this error and found out this may be related with the operator comparing two types of operand types. Here, 'read' is a stream variable and it has a data type of char and the operator is comparing two data types. I am pretty sure that all the errors while running this code arises from the operator comparing the two data types.
I used "g++ -g veg_par.cpp -o a.exe" for compilation.
How do I solve this error? Any help would be highly appreciated.
The first error says there is no match for 'operator!=' with operand types char and int
Not quite. Your problem is in this expression read != 0. It's attempting to compare an std::ifstream to an int. Since the former is a class type, overloaded operators must by considered, as well as any user defined conversions to int. There are no such conversions, and no operator!= defined. So you can't write such a comparison.
Now, that does't mean you use read in the condition of an if-statement. It just means you must use it in one of the supported ways. Those being if(read) and if(!read). std::ifstream has an operator! as well is allowing contextual conversions to bool.
Related
I have been trying to get this to where binary_search would answer the if statement as true or false. I have not had problems with that in the past, however I am having issues getting it to search the tempStr in the struct I have made up. What I am trying to achieve with binary_search is for it to say whether the product exists or not, and make the decision whether to even bother doing a for loop or not to actually go out and get the name of the product. I get errors when I execute, and I do not understand why.
The second issue I have incurred is that I want to replace the price that is already in the vector with one that the user inputs, however this incurs similar errors to that of the binary_search. This is something I had made in my naïve attempt to make this happen, any and all help will be greatly appreciated.
Will clarify if I am asked to do so.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct grocery{
string name;
string expdate;
string price;
};
int main(){
grocery tempProd;
string tempStr;
vector<grocery> produce;
cout << "What grocery product are you looking for?";
getline(cin, tempStr);
if(binary_search(produce.begin(), produce.end(), [&] (const grocery &p) {return p.name == tempStr;})){
cout << "Product has been found!" << endl;
for(int i= 0; i<produce.size(); i++){
if(produce[i].name == tempStr){
string tempStr2;
cout << "What is going to be the new price of the product?";
getline(cin, tempStr2);
replace_if(produce.begin(), produce.end(), produce[i].price,
[&] (const grocery &p) {return p.price == tempStr2;});
}
}
}
else{
cout << "Product does not exist." << endl;
}
}
I have mainly messed about with lambda for this, however it has not brought me much, if any success. Pointers also seemed like they would be the answer to me, but that did not do me much good either, and I just seem to find myself back here, at this location.
Here is how the struct is set up, and how the vector is set up since I know both of those are important.
Errors
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h: In instantiation of 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>]':
teste.cpp:45:117: required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: error: no match for 'operator<' (operand types are 'const main()::<lambda(const body&)>' and 'body')
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:64,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:454:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)'
454 | operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:454:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::pair<_T1, _T2>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:331:5: note: candidate: 'template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)'
331 | operator<(const reverse_iterator<_Iterator>& __x,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:331:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::reverse_iterator<_Iterator>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:369:5: note: candidate: 'template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)'
369 | operator<(const reverse_iterator<_IteratorL>& __x,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:369:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::reverse_iterator<_Iterator>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1163:5: note: candidate: 'template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)'
1163 | operator<(const move_iterator<_IteratorL>& __x,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1163:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::move_iterator<_IteratorL>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1169:5: note: candidate: 'template<class _Iterator> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)'
1169 | operator<(const move_iterator<_Iterator>& __x,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1169:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::move_iterator<_IteratorL>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6226:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)'
6226 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6226:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6239:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)'
6239 | operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6239:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6251:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)'
6251 | operator<(const _CharT* __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6251:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: mismatched types 'const _CharT*' and 'main()::<lambda(const body&)>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:46,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:208:3: note: candidate: 'bool std::operator<(const std::error_code&, const std::error_code&)'
208 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:208:31: note: no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const std::error_code&'
208 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:282:3: note: candidate: 'bool std::operator<(const std::error_condition&, const std::error_condition&)'
282 | operator<(const error_condition& __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:282:36: note: no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const std::error_condition&'
282 | operator<(const error_condition& __lhs,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\vector:67,
from teste.cpp:3:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1905:5: note: candidate: 'template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)' 1905 | operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1905:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note: 'const main()::<lambda(const body&)>' is not derived from 'const std::vector<_Tp, _Alloc>'
2259 | return __i != __last && !(__val < *__i);
| ~~~~~~~^~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h: In instantiation of 'void std::replace_if(_FIter, _FIter, _Predicate, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Predicate = std::__cxx11::basic_string<char>; _Tp = main()::<lambda(const body&)>]':
teste.cpp:53:82: required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:4401:12: error: no match for call to '(std::__cxx11::basic_string<char>) (body&)'
4401 | if (__pred(*__first))
| ~~~~~~^~~~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:4402:13: error: no match for 'operator=' (operand types are 'body' and 'const main()::<lambda(const body&)>')
4402 | *__first = __new_value;
| ~~~~~~~~~^~~~~~~~~~~~~
teste.cpp:9:8: note: candidate: 'body& body::operator=(const body&)'
9 | struct body{
| ^~~~
teste.cpp:9:8: note: no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const body&'
teste.cpp:9:8: note: candidate: 'body& body::operator=(body&&)'
teste.cpp:9:8: note: no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'body&&'
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Value = const main()::<lambda(const body&)>]':
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:979:14: required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2257:22: required from 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>]'
teste.cpp:45:117: required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: error: no match for 'operator<' (operand types are 'body' and 'const main()::<lambda(const body&)>')
65 | { return *__it < __val; }
| ~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:915:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
915 | operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:915:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: note: 'body' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
65 | { return *__it < __val; }
| ~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:922:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)'
922 | operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
| ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:922:5: note: template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: note: 'body' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
65 | { return *__it < __val; }
edit: yes I know the example doesn't have a grocery yet, but having a grocery or not has NOT caused a problem, as I am working with information from a text file, this is literally the bare minimum that causes the issues I am getting.
Even if the compiler error relating to binary_search is fixed (which is a bit tricky, since binary_search searches for a grocery and not a string), it won't help you. That is because binary_search requires the vector to be sorted to work correctly and it isn't, here.
Therefore, replace it with std::find_if, which does not have this requirement:
auto it = std::find_if (produce.begin(), produce.end(), [&] (const grocery &g) {return g.name == tempStr;});
Doing that means that you don't then need to search the vector again for the matching item since you already have an iterator pointing right at it. Instead, you can just do:
if (it != produce.end()){
cout << "Product has been found!" << endl;
cout << "What is going to be the new price of the product?";
string tempStr2;
getline(cin, tempStr2);
it->price = tempStr2;
}
else{
cout << "Product does not exist." << endl;
}
All in all, a better solution.
Live demo
This question already has answers here:
Error using custom operator< with std::less
(2 answers)
Closed 4 years ago.
I want to define a static map inside a class, which will have a structure as both it's key and value. I've read that a '<' operator is needed to be defined, therefore I've included that definition inside the struct itself(I've randomly defined it as I don't need any comparison anywhere in my program).
The below sample code doesn't compile.The compiler produces a lot of warnings and errors which I don't understand right now. (Please ignore the uninitialized values in the main function):
#include<string>
#include<iostream>
#include<map>
using namespace std;
struct RJNodeAddress
{
string m_ip;
string m_port;
bool operator<(const RJNodeAddress &l)
{
int l_size=l.m_ip.size();
int r_size=l.m_port.size();
return (l_size < r_size);
}
};
struct RJNodeDetails
{
string m_NodeType;
int m_appId;
};
class RJWebsocket
{
public:
static map<RJNodeAddress,RJNodeDetails> m_Nodes;
};
int main()
{
RJNodeAddress l_node;
RJNodeDetails l_nodeDetails = RJWebsocket::m_Nodes[l_node];
}
Compiler output
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h: In instantiation of âconstexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = RJNodeAddress]â:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_map.h:491:32: required from âstd::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = RJNodeAddress; _Tp = RJNodeDetails; _Compare = std::less<RJNodeAddress>; _Alloc = std::allocator<std::pair<const RJNodeAddress, RJNodeDetails> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = RJNodeDetails; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = RJNodeAddress]â
test.cpp:34:59: required from here
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: error: no match for âoperator<â (operand types are âconst RJNodeAddressâ and âconst RJNodeAddressâ)
{ return __x < __y; }
~~~~^~~~~
test.cpp:10:14: note: candidate: bool RJNodeAddress::operator<(const RJNodeAddress&) <near match>
bool operator<(const RJNodeAddress &l)
^~~~~~~~
test.cpp:10:14: note: passing âconst RJNodeAddress*â as âthisâ argument discards qualifiers
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_algobase.h:64:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/char_traits.h:39,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:40,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_pair.h:449:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_pair.h:449:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::pair<_T1, _T2>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_algobase.h:67:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/char_traits.h:39,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:40,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:305:5: note: candidate: template<class _Iterator> constexpr bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator<(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:305:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::reverse_iterator<_Iterator>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_algobase.h:67:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/char_traits.h:39,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:40,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:343:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator<(const reverse_iterator<_IteratorL>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:343:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::reverse_iterator<_Iterator>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_algobase.h:67:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/char_traits.h:39,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:40,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:1142:5: note: candidate: template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator<(const move_iterator<_IteratorL>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:1142:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::move_iterator<_IteratorL>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_algobase.h:67:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/char_traits.h:39,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:40,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:1148:5: note: candidate: template<class _Iterator> constexpr bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator<(const move_iterator<_Iterator>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_iterator.h:1148:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::move_iterator<_IteratorL>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:48:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:485:5: note: candidate: template<class _CharT, class _Traits> bool std::operator<(std::basic_string_view<_CharT, _Traits>, std::basic_string_view<_CharT, _Traits>)
operator< (basic_string_view<_CharT, _Traits> __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:485:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âRJNodeAddressâ is not derived from âstd::basic_string_view<_CharT, _Traits>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:48:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:491:5: note: candidate: template<class _CharT, class _Traits> bool std::operator<(std::basic_string_view<_CharT, _Traits>, std::__detail::__idt<std::basic_string_view<_CharT, _Traits> >)
operator< (basic_string_view<_CharT, _Traits> __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:491:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âRJNodeAddressâ is not derived from âstd::basic_string_view<_CharT, _Traits>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:48:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:497:5: note: candidate: template<class _CharT, class _Traits> bool std::operator<(std::__detail::__idt<std::basic_string_view<_CharT, _Traits> >, std::basic_string_view<_CharT, _Traits>)
operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/string_view:497:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âRJNodeAddressâ is not derived from âstd::basic_string_view<_CharT, _Traits>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5892:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5892:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::basic_string<_CharT, _Traits, _Alloc>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5905:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5905:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::basic_string<_CharT, _Traits, _Alloc>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:52:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5917:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const _CharT* __lhs,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/basic_string.h:5917:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: mismatched types âconst _CharT*â and âRJNodeAddressâ
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/ios_base.h:46:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/ios:42,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/ostream:38,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/iostream:39,
from test.cpp:2:
/opt/rh/devtoolset-7/root/usr/include/c++/7/system_error:208:3: note: candidate: bool std::operator<(const std::error_code&, const std::error_code&)
operator<(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/system_error:208:3: note: no known conversion for argument 1 from âconst RJNodeAddressâ to âconst std::error_code&â
/opt/rh/devtoolset-7/root/usr/include/c++/7/system_error:282:3: note: candidate: bool std::operator<(const std::error_condition&, const std::error_condition&)
operator<(const error_condition& __lhs,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/system_error:282:3: note: no known conversion for argument 1 from âconst RJNodeAddressâ to âconst std::error_condition&â
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:808:5: note: candidate: template<class _Tp, class _Up> constexpr std::__optional_relop_t<decltype ((declval<_Tp>() < declval<_Up>()))> std::operator<(const std::optional<_Tp>&, const std::optional<_Up>&)
operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:808:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::optional<_Tp>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:861:5: note: candidate: template<class _Tp> constexpr bool std::operator<(const std::optional<_Tp>&, std::nullopt_t)
operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:861:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::optional<_Tp>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:866:5: note: candidate: template<class _Tp> constexpr bool std::operator<(std::nullopt_t, const std::optional<_Tp>&)
operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:866:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::optional<_Tp>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:926:5: note: candidate: template<class _Tp, class _Up> constexpr std::__optional_relop_t<decltype ((declval<_Tp>() < declval<_Up>()))> std::operator<(const std::optional<_Tp>&, const _Up&)
operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:926:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::optional<_Tp>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:932:5: note: candidate: template<class _Tp, class _Up> constexpr std::__optional_relop_t<decltype ((declval<_Up>() < declval<_Tp>()))> std::operator<(const _Up&, const std::optional<_Tp>&)
operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/optional:932:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::optional<_Tp>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/tuple:39:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:40,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/array:262:5: note: candidate: template<class _Tp, long unsigned int _Nm> bool std::operator<(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/array:262:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::array<_Tp, _Nm>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/node_handle.h:40:0,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:72,
from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/tuple:1410:5: note: candidate: template<class ... _TElements, class ... _UElements> constexpr bool std::operator<(const std::tuple<_Tps ...>&, const std::tuple<_Elements ...>&)
operator<(const tuple<_TElements...>& __t,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/tuple:1410:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::tuple<_Tps ...>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:60:0,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:1543:5: note: candidate: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_tree.h:1543:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:61:0,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_map.h:1397:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_map.h:1397:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::map<_Key, _Tp, _Compare, _Alloc>â
{ return __x < __y; }
~~~~^~~~~
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/map:62:0,
from test.cpp:3:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_multimap.h:1062:5: note: candidate: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
^~~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_multimap.h:1062:5: note: template argument deduction/substitution failed:
In file included from /opt/rh/devtoolset-7/root/usr/include/c++/7/string:48:0,
from test.cpp:1:
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/stl_function.h:386:20: note: âconst RJNodeAddressâ is not derived from âconst std::multimap<_Key, _Tp, _Compare, _Alloc>â
{ return __x < __y; }
~~~~^~~~~
As keys in std::map are const your operator< method must be const as well:
bool operator<(const RJNodeAddress &l) const
// ^ here
it should be const anyway as it does not modify the object but in this case missing it leads to compilation errors.
I have overloaded allocator for most of the stl containers below is the code. The code is from here
#include <iostream>
#include <vector>
#include <list>
namespace outside
{
template<typename T>
struct MyAllocator
{
typedef typename std::allocator<T>::value_type value_type;
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::const_pointer const_pointer;
typedef typename std::allocator<T>::reference reference;
typedef typename std::allocator<T>::const_reference const_reference;
typedef typename std::allocator<T>::size_type size_type;
pointer allocate (size_type n, typename std::allocator<void>::const_pointer hint=0)
{
std::cerr << "allocate..." << std::endl;
return std::allocator<T>{}.allocate(n, hint);
}
void deallocate (pointer p, size_type n)
{
std::cerr << "deallocate..." << std::endl;
return std::allocator<T>{}.deallocate(p, n);
}
template <class Type> struct rebind
{
typedef MyAllocator<Type> other;
};
MyAllocator()
{
}
MyAllocator(const MyAllocator<T>& other )
{
}
template< class U >
MyAllocator( const MyAllocator<U>& other )
{
}
};
} // namespace outside
namespace outer
{
template<class T>
using vector = ::std::vector<T, outside::MyAllocator<T>>;
template<class T>
using list = ::std::list<T, outside::MyAllocator<T>>;
}
int main()
{
outer::vector<int> myVector1;
outer::vector<int> myVector2;
myVector1.push_back(10);
myVector1.push_back(20);
// myVector2 = myVector1; // Getting compilation error
for (auto i = myVector1.begin(); i != myVector1.end(); ++i) {
std::cout << *i << ' ';
}
std::list<int> myList1;
std::list<int> myList2;
myList2 = myList1; // // No compilation error
}
Copying one vector to another vector is giving me error. While for the same code copying one list to another list is working fine.
Compilation error
included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc: In instantiation of 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = outside::MyAllocator<int>]':
prog.cpp:59:15: required from here
/usr/include/c++/5/bits/vector.tcc:176:37: error: no match for 'operator!=' (operand types are 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' and 'const _Tp_alloc_type {aka const outside::MyAllocator<int>}')
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/iosfwd:40:0,
from /usr/include/c++/5/ios:38,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^
/usr/include/c++/5/bits/postypes.h:221:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::fpos<_StateT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:227:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/include/c++/5/bits/stl_pair.h:227:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::pair<_T1, _T2>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:304:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:304:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::reverse_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:354:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:354:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::reverse_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1077:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:1077:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::move_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1083:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
operator!=(const move_iterator<_Iterator>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:1083:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::move_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:41:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/allocator.h:140:5: note: candidate: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^
/usr/include/c++/5/bits/allocator.h:140:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::allocator<_CharT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:41:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/allocator.h:146:5: note: candidate: template<class _Tp> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^
/usr/include/c++/5/bits/allocator.h:146:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::allocator<_CharT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4948:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4948:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4960:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4960:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: mismatched types 'const _CharT*' and 'outside::MyAllocator<int>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4972:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4972:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/ios_base.h:46:0,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/system_error:311:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^
/usr/include/c++/5/system_error:311:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_code&'
/usr/include/c++/5/system_error:315:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^
/usr/include/c++/5/system_error:315:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_code&'
/usr/include/c++/5/system_error:319:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^
/usr/include/c++/5/system_error:319:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_condition&'
/usr/include/c++/5/system_error:323:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^
/usr/include/c++/5/system_error:323:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_condition&'
In file included from /usr/include/c++/5/bits/locale_facets.h:48:0,
from /usr/include/c++/5/bits/basic_ios.h:37,
from /usr/include/c++/5/ios:44,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^
/usr/include/c++/5/bits/streambuf_iterator.h:210:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::istreambuf_iterator<_CharT, _Traits>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/vector:64:0,
from prog.cpp:2:
/usr/include/c++/5/bits/stl_vector.h:1535:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/include/c++/5/bits/stl_vector.h:1535:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::vector<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/list:63:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_list.h:291:5: note: candidate: template<class _Val> bool std::operator!=(const std::_List_iterator<_Tp>&, const std::_List_const_iterator<_Val>&)
operator!=(const _List_iterator<_Val>& __x,
^
/usr/include/c++/5/bits/stl_list.h:291:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::_List_iterator<_Tp>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/list:63:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_list.h:1843:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
^
/usr/include/c++/5/bits/stl_list.h:1843:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::list<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
I am trying to understand why this is the case and solution for this problem ?
The key part of the exception is hundreds of lines into it:
In file included from /usr/local/include/c++/5.2.0/vector:64:0,
from main.cpp:2: /usr/local/include/c++/5.2.0/bits/stl_vector.h:1535:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/local/include/c++/5.2.0/bits/stl_vector.h:1535:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.2.0/vector:69:0,
from main.cpp:2:
/usr/local/include/c++/5.2.0/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::vector<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
vector::operator= requires vector::operator!=, which requires allocator::operator!=, which doesn't exist. Without it, the function can't be created. Simply add an operator!= to your allocator. While you're at it, also make sure you have all the other required allocator functions. If memory serves me right, you're missing a fair number.
Change the definition to struct MyAllocator: public std::allocator<T> and the code will compile.
I've been curious about why one error can cause the compiler to generate a very long list of error messages. The following example is the result of an erroneous comparison between an element of a vector<string> and NULL at line 100 of main.cpp under GCC 4.8.1:
> g++ -g3 -std=c++11 main.cpp functions.cpp
main.cpp: In function ‘int main()’:
main.cpp:100:24: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ and ‘long int’)
if(args[1] == NULL) {
^
main.cpp:100:24: note: candidates are:
In file included from /usr/include/c++/4.8/iosfwd:40:0,
from /usr/include/c++/4.8/ios:38,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/postypes.h:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^
/usr/include/c++/4.8/bits/postypes.h:216:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::fpos<_StateT>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note: template<class _T1, class _T2> constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/include/c++/4.8/bits/stl_pair.h:214:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::pair<_T1, _T2>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator==(const reverse_iterator<_Iterator>& __x,
^
/usr/include/c++/4.8/bits/stl_iterator.h:291:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator==(const reverse_iterator<_IteratorL>& __x,
^
/usr/include/c++/4.8/bits/stl_iterator.h:341:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:1031:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
operator==(const move_iterator<_IteratorL>& __x,
^
/usr/include/c++/4.8/bits/stl_iterator.h:1031:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::move_iterator<_Iterator>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:1037:5: note: template<class _Iterator> bool std::operator==(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
operator==(const move_iterator<_Iterator>& __x,
^
/usr/include/c++/4.8/bits/stl_iterator.h:1037:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::move_iterator<_Iterator>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:41: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/allocator.h:128:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator==(const allocator<_T1>&, const allocator<_T2>&)
^
/usr/include/c++/4.8/bits/allocator.h:128:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:41: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/allocator.h:133:5: note: template<class _Tp> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator==(const allocator<_Tp>&, const allocator<_Tp>&)
^
/usr/include/c++/4.8/bits/allocator.h:133:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::allocator<_CharT>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:52: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.8/bits/basic_string.h:2486:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘long int’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:52: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
operator==(const basic_string<_CharT>& __lhs,
^
/usr/include/c++/4.8/bits/basic_string.h:2493:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: mismatched types ‘const std::basic_string<_CharT>’ and ‘long int’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:52: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator==(const _CharT* __lhs,
^
/usr/include/c++/4.8/bits/basic_string.h:2507:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: mismatched types ‘const _CharT*’ and ‘std::basic_string<char>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/string:52: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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/4.8/bits/basic_string.h:2519:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: mismatched types ‘const _CharT*’ and ‘long int’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/bits/locale_facets.h:48:0,
from /usr/include/c++/4.8/bits/basic_ios.h:37,
from /usr/include/c++/4.8/ios:44,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
^
/usr/include/c++/4.8/bits/streambuf_iterator.h:204:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/vector:64:0,
from main.h:16,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_vector.h:1403:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/include/c++/4.8/bits/stl_vector.h:1403:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::vector<_Tp, _Alloc>’
if(args[1] == NULL) {
^
In file included from /usr/include/c++/4.8/x86_64-suse-linux/bits/c++allocator.h:33:0,
from /usr/include/c++/4.8/bits/allocator.h:46,
from /usr/include/c++/4.8/string:41,
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
^
/usr/include/c++/4.8/ext/new_allocator.h:139:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
^
/usr/include/c++/4.8/bits/stl_iterator.h:811:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’
if(args[1] == NULL) {
^
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 main.h:13,
from main.cpp:12:
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
^
/usr/include/c++/4.8/bits/stl_iterator.h:805:5: note: template argument deduction/substitution failed:
In file included from /usr/include/unistd.h:226:0,
from main.h:20,
from main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
if(args[1] == NULL) {
^
This can make it very hard to understand the problem. What is the reason for this behavior?
If you have an expression of the form vec == NULL, the compiler applies ADL to find an appropriate operator== to call. If it can't find a single one it outputs all operator== candidates found in all namespaces considered, and the reason they did not fit here.
One of those can look like this:
In file included from [..] main.cpp:12:
[..]:216:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^
[..]:216:5: note: template argument deduction/substitution failed:
In file included from [..] main.cpp:12:
main.cpp:100:27: note: ‘__gnu_cxx::__alloc_traits<std::allocator<std::basic_string<char> > >::value_type {aka std::basic_string<char>}’ is not derived from ‘const std::fpos<_StateT>’
if(args[1] == NULL) {
^
We couldn't match the first parameter to the first argument as std::string isn't derived from any fpos-specialization (also std::string doesn't have any appropriate conversion operators, but that isn't mentioned here). If you defined an operator== that can't be found/called, wouldn't you like to know the reason from the compilers view? (Especially if you don't have a clue yourself)
This can be very useful at times. When it isn't, ignore it. In most cases C++-programmers immediately see and understand what causes the error message (and its enourmos note-section).
This may help in situations like this (you got a large volume of template related message).
Whether your are using make or directly using the compiler, try to pipe the stderr to a file in my example
make 2> error.log
The use any editor open the error.log file. Search for file name of the source code you wrote. There should only be one or two lines that are interesting to you. For example:
mysourcefile.cpp:51:20: required from here
This is where the problem happened; look harder here.
temp_holder.clear();
temp_holder << n;
n_str = temp_holder.str();
int f = count(n_str.begin(), n_str.end(), a);
That's my code, and this is g++ output:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h: In instantiation of ‘typename std::iterator_traits<_InputIterator>::difference_type std::count(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _Tp = std::basic_string<char>; typename std::iterator_traits<_InputIterator>::difference_type = long int]’:
trintatres.cpp:44:50: required from here
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<char*, std::basic_string<char> >() == __value’
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: candidates are:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iosfwd:42:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:39,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/postypes.h:218:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::fpos<_StateT>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:65:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:212:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:212:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::pair<_T1, _T2>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:68:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:293:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:293:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:68:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:343:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:343:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::reverse_iterator<_IteratorL>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:43:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/allocator.h:119:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/allocator.h:119:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::allocator<_T1>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:43:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/allocator.h:124:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/allocator.h:124:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::allocator<_Tp1>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:54:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2483:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2483:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:54:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2490:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2490:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::basic_string<_CharT>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:54:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2504:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2504:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const _CharT*’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:54:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2516:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_string.h:2516:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_facets.h:50:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/basic_ios.h:39,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:45,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/streambuf_iterator.h:206:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/streambuf_iterator.h:206:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const std::istreambuf_iterator<_CharT, _Traits>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/x86_64-unknown-linux-gnu/bits/c++allocator.h:34:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/allocator.h:48,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/string:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:43,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ext/new_allocator.h:129:5: note: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ext/new_allocator.h:129:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const __gnu_cxx::new_allocator<_Tp>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:68:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:813:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:813:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ and ‘char’
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:68:0,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from trintatres.cpp:1:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:807:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_iterator.h:807:5: note: template argument deduction/substitution failed:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/algorithm:63:0,
from trintatres.cpp:5:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/bits/stl_algo.h:4656:2: note: mismatched types ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’ and ‘char’
I honestly think I'm using std::count from correctly, according to cplusplus.com.
Any idea of what I'm doing wrong?
EDIT
Full code:
#include <iostream>
#include <string>
#include <sstream>
#include <climits>
#include <algorithm>
using namespace std;
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
string a;
cin >> a;
int c;
cin >> c;
int q;
cin >> q;
string cases[q];
int max_case = INT_MIN;
int temp;
for (int i = 0; i < q; i++) {
cin >> temp;
max_case = max(temp, max_case);
stringstream current_case_holder;
current_case_holder << temp;
cases[i] = current_case_holder.str();
}
int sequence[max_case];
int n;
stringstream temp_holder;
string n_str;
for (int i = 0; i < max_case; i++) {
n = 1;
while (true) {
temp_holder.clear();
temp_holder << n;
n_str = temp_holder.str();
int f = count(n_str.begin(), n_str.end(), a);
//}
n++;
}
}
return 0;
}
The first error tells you all you need to know:
In instantiation of ‘typename std::iterator_traits<_InputIterator>::difference_type std::count(_IIter, _IIter, const _Tp&)
[with _IIter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >;
_Tp = std::basic_string<char>;
typename std::iterator_traits<_InputIterator>::difference_type = long int]’
This indicates that you're passing a std::string object for the third argument to std::count, which is wrong; you need to pass a char object instead, since you're effectively iterating over a std::string, and std::string::value_type is char.