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
Related
Hey guys, I'm a beginner who is trying to learn C++ and I'm trying to do a lab that asks to output the number of times the character appears in the string, with test cases for calrifcation in the picture above. I was trying to do the lab but I came upon a error message that I can not figure out what's the problem is at exacly.
I was trying to ask for an input of the input character and the input string and was trying to make a for loop that goes in every letter of the string input and calculate how many times the input character match with the same character in the string.
this is my code below:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string inputVal;
string inputWord;
int outputVal =0 ;
cin >> inputVal;
cout << inputVal;
cin >> inputWord;
cout << inputWord;
for (int i=0; i <= inputWord.size(); ++i){
if (inputWord.at(i) == inputVal) {
++outputVal;
}
}
if (outputVal ==1) {
cout << outputVal << " " << inputVal<<endl;
}
else {
cout << outputVal << " " << inputVal<<"'s" <<endl;
}
}
this is the error message below:
main.cpp: In function ‘int main()’:
main.cpp:16:20: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
16 | for (int i=0; i <= inputWord.size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~
main.cpp:17:25: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
17 | if (inputWord.at(i) == inputVal) {
| ~~~~~~~~~~~~~~~ ^~ ~~~~~~~~
| | |
| | std::string {aka std::__cxx11::basic_string<char>}
| __gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type {aka char}
In file included from /usr/include/c++/9/iosfwd:40,
from /usr/include/c++/9/ios:38,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/postypes.h:222:5: note: candidate: ‘template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)’
222 | operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
| ^~~~~~~~
/usr/include/c++/9/bits/postypes.h:222:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::fpos<_StateT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_pair.h:448:5: note: candidate: ‘template<class _T1, class _T2> constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)’
448 | operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
| ^~~~~~~~
/usr/include/c++/9/bits/stl_pair.h:448:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::pair<_T1, _T2>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:325:5: note: candidate: ‘template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)’
325 | operator==(const reverse_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:325:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:363:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)’
363 | operator==(const reverse_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:363:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::reverse_iterator<_Iterator>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:1136:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> bool std::operator==(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’
1136 | operator==(const move_iterator<_IteratorL>& __x,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1136:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::move_iterator<_IteratorL>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:1142:5: note: candidate: ‘template<class _Iterator> bool std::operator==(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)’
1142 | operator==(const move_iterator<_Iterator>& __x,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1142:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::move_iterator<_IteratorL>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:41,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/allocator.h:167:5: note: candidate: ‘template<class _T1, class _T2> bool std::operator==(const std::allocator<_CharT>&, const std::allocator<_T2>&)’
167 | operator==(const allocator<_T1>&, const allocator<_T2>&)
| ^~~~~~~~
/usr/include/c++/9/bits/allocator.h:167:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::allocator<_CharT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6144: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>&)’
6144 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6144:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6152:5: note: candidate: ‘template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::__cxx11::basic_string<_CharT>&, const std::__cxx11::basic_string<_CharT>&)’
6152 | operator==(const basic_string<_CharT>& __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6152:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::__cxx11::basic_string<_CharT>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6166:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)’
6166 | operator==(const _CharT* __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6166:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const _CharT*’ and ‘char’
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/basic_string.h:6178:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)’
6178 | operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6178:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/ios_base.h:46,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/system_error:292:3: note: candidate: ‘bool std::operator==(const std::error_code&, const std::error_code&)’
292 | operator==(const error_code& __lhs, const error_code& __rhs) noexcept
| ^~~~~~~~
/usr/include/c++/9/system_error:292:32: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_code&’
292 | operator==(const error_code& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:297:3: note: candidate: ‘bool std::operator==(const std::error_code&, const std::error_condition&)’
297 | operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
| ^~~~~~~~
/usr/include/c++/9/system_error:297:32: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_code&’
297 | operator==(const error_code& __lhs, const error_condition& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:304:3: note: candidate: ‘bool std::operator==(const std::error_condition&, const std::error_code&)’
304 | operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
| ^~~~~~~~
/usr/include/c++/9/system_error:304:37: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_condition&’
304 | operator==(const error_condition& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:311:3: note: candidate: ‘bool std::operator==(const std::error_condition&, const std::error_condition&)’
311 | operator==(const error_condition& __lhs,
| ^~~~~~~~
/usr/include/c++/9/system_error:311:37: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::error_condition&’
311 | operator==(const error_condition& __lhs,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/c++/9/bits/locale_facets.h:48,
from /usr/include/c++/9/bits/basic_ios.h:37,
from /usr/include/c++/9/ios:44,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/streambuf_iterator.h:208:5: note: candidate: ‘template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)’
208 | operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
| ^~~~~~~~
/usr/include/c++/9/bits/streambuf_iterator.h:208:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::istreambuf_iterator<_CharT, _Traits>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/tuple:39,
from /usr/include/c++/9/bits/unique_ptr.h:37,
from /usr/include/c++/9/bits/locale_conv.h:41,
from /usr/include/c++/9/locale:43,
from /usr/include/c++/9/iomanip:43,
from main.cpp:2:
/usr/include/c++/9/array:252:5: note: candidate: ‘template<class _Tp, long unsigned int _Nm> bool std::operator==(const std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)’
252 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
| ^~~~~~~~
/usr/include/c++/9/array:252:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::array<_Tp, _Nm>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/unique_ptr.h:37,
from /usr/include/c++/9/bits/locale_conv.h:41,
from /usr/include/c++/9/locale:43,
from /usr/include/c++/9/iomanip:43,
from main.cpp:2:
/usr/include/c++/9/tuple:1419:5: note: candidate: ‘template<class ... _TElements, class ... _UElements> constexpr bool std::operator==(const std::tuple<_Tps ...>&, const std::tuple<_Elements ...>&)’
1419 | operator==(const tuple<_TElements...>& __t,
| ^~~~~~~~
/usr/include/c++/9/tuple:1419:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::tuple<_Tps ...>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
from /usr/include/c++/9/locale:43,
from /usr/include/c++/9/iomanip:43,
from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:715:5: note: candidate: ‘template<class _Tp, class _Dp, class _Up, class _Ep> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, _Ep>&)’
715 | operator==(const unique_ptr<_Tp, _Dp>& __x,
| ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:715:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
from /usr/include/c++/9/locale:43,
from /usr/include/c++/9/iomanip:43,
from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:721:5: note: candidate: ‘template<class _Tp, class _Dp> bool std::operator==(const std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)’
721 | operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
| ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:721:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const std::unique_ptr<_Tp, _Dp>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/locale_conv.h:41,
from /usr/include/c++/9/locale:43,
from /usr/include/c++/9/iomanip:43,
from main.cpp:2:
/usr/include/c++/9/bits/unique_ptr.h:726:5: note: candidate: ‘template<class _Tp, class _Dp> bool std::operator==(std::nullptr_t, const std::unique_ptr<_Tp, _Dp>&)’
726 | operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
| ^~~~~~~~
/usr/include/c++/9/bits/unique_ptr.h:726:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} is not derived from ‘const std::unique_ptr<_Tp, _Dp>’
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/string:41,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/allocator.h:155:7: note: candidate: ‘bool std::operator==(const std::allocator<char>&, const std::allocator<char>&)’
155 | operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
| ^~~~~~~~
/usr/include/c++/9/bits/allocator.h:155:18: note: no known conversion for argument 1 from ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} to ‘const std::allocator<char>&’
155 | operator==(const allocator&, const allocator&) _GLIBCXX_NOTHROW
| ^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:883: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>&)’
883 | operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:883:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/bits/stl_iterator.h:890: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>&)’
890 | operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:890:5: note: template argument deduction/substitution failed:
main.cpp:17:28: note: mismatched types ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ and ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’}
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/c++allocator.h:33,
from /usr/include/c++/9/bits/allocator.h:46,
from /usr/include/c++/9/string:41,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from main.cpp:1:
/usr/include/c++/9/ext/new_allocator.h:166:2: note: candidate: ‘template<class _Up> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<char>&, const __gnu_cxx::new_allocator<_Tp>&)’
166 | operator==(const new_allocator&, const new_allocator<_Up>&)
| ^~~~~~~~
/usr/include/c++/9/ext/new_allocator.h:166:2: note: template argument deduction/substitution failed:
main.cpp:17:28: note: ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
17 | if (inputWord.at(i) == inputVal) {
| ^~~~~~~~
You first need only get a single character: use std::cin >>.
Then you need to get the entire line of input: use getline().
#include <iostream>
#include <string>
int main()
{
char c;
std::string s;
std::cin >> std::ws >> c; // get the character to count
getline( std::cin, s ); // get the rest of the text
}
That std::ws there skips any leading whitespace. That may not be correct for your assignment. (Is it possible that the character to search is a space or tab?)
I personally like getting prompted, so I would put something like
std::cout << "c text? ";
at the top of the program, but it is not necessary.
Now all you need is to count the cs:
int count = 0;
for (size_t n = 0; n < s.size(); n++)
if (s[n] == c)
...
Also, here is a short lecture on names:
Things like “input value” and “output value” are not very specific. You should prefer more specific names.
In some cases, however, non-specific names are more correct. For these, use common names, like I did for c (character) and s (string). (Other common meta-syntactic names are ch (character) and str (string).) These names instantly signal that the name of the object is not important, but also lets the user know what kind of object it is.
We care less that the output variable is to be output, but what it is. In this case it is a count. Hence its new name: count. When we std::cout the count it is pretty clear that it is part of the output.
Finally, right at the start you should get used to the idea of just typing std:: right at the front of Standard Library objects and not using using namespace std. It does seem a little overboard to do all that typing at first, but soon enough those five characters will just appear on your screen when you think them. Doing so will save you a lot of grief later when you inadvertently name something that also exists in the Standard Library.
Bonus: reading error messages
The compiler is verbose when complaining, barfing sometimes for more scrollback buffer than available in your terminal.
Whenever you get an error, pay close attention to the first one, fix it, and recompile. Take errors one at a time like that.
Your first error is:
main.cpp:16:20: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
16 | for (int i=0; i <= inputWord.size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~
Reading it we see that it is complaining that int and long unsigned int have different “signedness” and you are comparing them.
This is an obnoxiousness that you need to deal with when using for loops (because the problem it is complaining about is a valid issue). The easy fix is to just use size_t as your index type, as I did in the example above.
The next error you get is:
main.cpp:17:25: error: no match for ‘operator==’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type’ {aka ‘char’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
17 | if (inputWord.at(i) == inputVal) {
| ~~~~~~~~~~~~~~~ ^~ ~~~~~~~~
| | |
| | std::string {aka std::__cxx11::basic_string<char>}
| __gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type {aka char}
Here the compiler is saying it do an == comparison that has a char on the left-hand side and a std::string on the right-hand side. It even draws you a little picture showing the code and what each item is.
You can look back and see that inputVal is indeed a std::string, and a character .at(i) is not a string. This is a hint that you might need to revisit what type of thing inputVal is.
In the code I presented above we changed inputVal to c, complete with the new type of char. This makes more sense for the algorithm, since c only needs to be a single character anyway.
It does get easier. I promise!
I'm using cmake to build a c++ project.
cmake -S . -B outputDir
cmake --build outputDir
When there's a compilation error, cmake --build complains with a large list of "note" messages, making the terminal too much verbose.
For example, the following program:
#include <iostream>
int main()
{
std::cout < "Hello world\n"; // Error: should be <<
return 0;
}
Produces:
/home/user/githubRepos/cumath/main.cpp: In function ‘int main()’:
/home/user/githubRepos/cumath/main.cpp:7:15: error: no match for ‘operator<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [13]’)
7 | std::cout < "Hello world\n";
| ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
| | |
| | const char [13]
| std::ostream {aka std::basic_ostream<char>}
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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)
| ^~~~~~~~
/usr/include/c++/9/bits/stl_pair.h:454:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::pair<_T1, _T2>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:331:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:369:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::reverse_iterator<_Iterator>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1163:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:67,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/ios:40,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/stl_iterator.h:1169:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::move_iterator<_IteratorL>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6226:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6239:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: ‘std::ostream’ {aka ‘std::basic_ostream<char>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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,
| ^~~~~~~~
/usr/include/c++/9/bits/basic_string.h:6251:5: note: template argument deduction/substitution failed:
/home/user/githubRepos/cumath/main.cpp:7:17: note: mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
7 | std::cout < "Hello world\n";
| ^~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/ios_base.h:46,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/ostream:38,
from /usr/include/c++/9/iostream:39,
from /home/user/githubRepos/cumath/main.cpp:1:
/usr/include/c++/9/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
| ^~~~~~~~
/usr/include/c++/9/system_error:208:31: note: no known conversion for argument 1 from ‘std::ostream’ {aka ‘std::basic_ostream<char>’} to ‘const std::error_code&’
208 | operator<(const error_code& __lhs, const error_code& __rhs) noexcept
| ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/9/system_error:282:3: note: candidate: ‘bool std::operator<(const std::error_condition&, const std::error_condition&)’
282 | operator<(const error_condition& __lhs,
| ^~~~~~~~
/usr/include/c++/9/system_error:282:36: note: no known conversion for argument 1 from ‘std::ostream’ {aka ‘std::basic_ostream<char>’} to ‘const std::error_condition&’
282 | operator<(const error_condition& __lhs,
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
By default, the only lines I'm interested in are the first lines:
/home/user/githubRepos/cumath/main.cpp: In function ‘int main()’:
/home/user/githubRepos/cumath/main.cpp:7:15: error: no match for ‘operator<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const char [13]’)
Is there a way to remove the "note" level in cmake --build?
I've tried without luck:
cmake --build outputDir --log-level=WARNING
and
cmake --build outputDir -fcompare-debug-second
My cmake version is: 3.16.3
Thanks!
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.
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.
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.