一个C++新手写的关于C++模板和迭代器的代码错误
A code error about template and iterator of C++ made by a C++ new learner
作为一个C++新手,在尝试学习C++的模板和迭代器的时候,用C++的模板和迭代器写了一个判断一个数是否在数组中的函数。代码如下:
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
for(;begin != end; begin++){
if(*begin == target){
return true;
}
}
return false;
}
int main(void){
vector<int> array{1,2,3,4,6,7};
if(find(array.begin(),array.end(), 7)){
cout << "find!";
}
else{
cout << "not found!";
}
}
当我运行上面的代码时,我会遇到这样的错误:
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^~~~~~
test.cpp:2631:94: error: expression list treated as compound expression in initializer [-fpermissive]
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^
test.cpp:2631:95: error: expected ';' before '{' token
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^
;
test.cpp: In function 'int main()':
test.cpp:2643:8: error: reference to 'find' is ambiguous
if(find(array.begin(),array.end(), 7)){
^~~~
In file included from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62,
from test.cpp:3:
C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:3897:5: note: candidates are: 'template<class _IIter, class _Tp> _IIter std::find(_IIter, _IIter, const _Tp&)'
find(_InputIterator __first, _InputIterator __last,
^~~~
In file included from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/locale_facets.h:48,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/basic_ios.h:37,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:44,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from test.cpp:1:
C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/streambuf_iterator.h:368:5: note: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)'
find(istreambuf_iterator<_CharT> __first,
^~~~
test.cpp:2631:43: note: 'template<class T> bool find<T>'
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^~~~~~~~
我只是不知道函数有什么问题,真诚地希望有人能提供帮助!
您应该在函数参数中的迭代器类型之前添加类型名。此外,如果您为函数使用名称 find 并在代码中包含 std 命名空间,则会发生冲突。因此,您应该为该函数使用另一个名称,或者为标准函数使用范围解析运算符。我在这里为您修复:
template<typename T>
bool findElement(typename std::vector<T>::iterator &begin,
typename std::vector<T>::iterator &end, T target){
for(;begin != end; begin++){
if(*begin == target){
return true;
}
}
return false;
}
int main(void){
std::vector<int> array{1,2,3,4,6,7};
auto it1 = array.begin();
auto it2 = array.end();
if(findElement(it1, it2, 7)){
std::cout << "find!";
}
else{
std::cout << "not found!";
}
}
作为一个C++新手,在尝试学习C++的模板和迭代器的时候,用C++的模板和迭代器写了一个判断一个数是否在数组中的函数。代码如下:
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
for(;begin != end; begin++){
if(*begin == target){
return true;
}
}
return false;
}
int main(void){
vector<int> array{1,2,3,4,6,7};
if(find(array.begin(),array.end(), 7)){
cout << "find!";
}
else{
cout << "not found!";
}
}
当我运行上面的代码时,我会遇到这样的错误:
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^~~~~~
test.cpp:2631:94: error: expression list treated as compound expression in initializer [-fpermissive]
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^
test.cpp:2631:95: error: expected ';' before '{' token
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^
;
test.cpp: In function 'int main()':
test.cpp:2643:8: error: reference to 'find' is ambiguous
if(find(array.begin(),array.end(), 7)){
^~~~
In file included from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:62,
from test.cpp:3:
C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:3897:5: note: candidates are: 'template<class _IIter, class _Tp> _IIter std::find(_IIter, _IIter, const _Tp&)'
find(_InputIterator __first, _InputIterator __last,
^~~~
In file included from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/locale_facets.h:48,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/basic_ios.h:37,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:44,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
from C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
from test.cpp:1:
C:/LLVM/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/streambuf_iterator.h:368:5: note: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)'
find(istreambuf_iterator<_CharT> __first,
^~~~
test.cpp:2631:43: note: 'template<class T> bool find<T>'
template<typename T> bool find(vector<T>::iterator &begin, vector<T>::iterator &end, T target){
^~~~~~~~
我只是不知道函数有什么问题,真诚地希望有人能提供帮助!
您应该在函数参数中的迭代器类型之前添加类型名。此外,如果您为函数使用名称 find 并在代码中包含 std 命名空间,则会发生冲突。因此,您应该为该函数使用另一个名称,或者为标准函数使用范围解析运算符。我在这里为您修复:
template<typename T>
bool findElement(typename std::vector<T>::iterator &begin,
typename std::vector<T>::iterator &end, T target){
for(;begin != end; begin++){
if(*begin == target){
return true;
}
}
return false;
}
int main(void){
std::vector<int> array{1,2,3,4,6,7};
auto it1 = array.begin();
auto it2 = array.end();
if(findElement(it1, it2, 7)){
std::cout << "find!";
}
else{
std::cout << "not found!";
}
}