- 论坛徽章:
- 0
|
最近在看essential C++,在linux下eclipse平台调试,尝试了书上的一个例子,但是调试总会报错,望各位大大指教。
代码如下:- #include <iostream>
- #include<string>
- #include<vector>
- #include<algorithm>
- using namespace std;
- template <typename T>
- T* vfind(T *first,T *last, T value)
- {
- if(!first||!last)
- {
- return 0;
- }
- for(;first!=last;++first)
- {
- if(*first==value)
- {
- return first;
- }
- }
- return 0;
- }
- int main() {
- int ia[5]={1,2,3,4,5};
- double da[5]={1.1,2.5,3.6,4.8,5.0};
- vector<int> via(ia,ia+5);
- vector<double> vda(da,da+5);
- // string sa[5]={"hello","hi","nihao","xiexie","over"};
- int *pi=vfind(ia,ia+5,ia[1]);
- double *pd=vfind(da,da+5,da[2]);
- int *vpi=vfind(&via[0],&via[5],via[1]);
- double *vpd=vfind(&vda[0],&vda[5],vda[1]);
- // string *ps=vfind(sa,sa+5,"nihao");
- cout<<"pi="<<*pi<<endl
- <<"pd="<<*pd<<endl
- <<"vpi="<<*vpi<<endl
- <<"vpd="<<*vpd<<endl;
- return 0;
- }
复制代码 用string类型时系统会报错,且错误很奇怪:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
#0 * vfind(#0 *, #0 *, #0)
' chapter3.cpp /chapter3/src line 44 Semantic Error
make: *** [src/chapter3.o] 错误 1 chapter3 C/C++ Problem
错误:对‘vfind(std::string [5], std::string*, const char [6])’的调用没有匹配的函数 chapter3.cpp /chapter3/src line 44 C/C++ Problem
附注:template<class T> T* vfind(T*, T*, T) chapter3.cpp /chapter3/src line 16 C/C++ Problem
请各位指教。 |
|