我一直在努力编译并玩弄了&符号,但仍无法弄清楚错误是什么。有什么想法吗?
qsort.cc:22:23: error: no matching function for call to ‘qsort<int>::quicksort(std::vector<int, std::allocator<int> >*)’
qsort.cc:22:23: note: candidate is:
qsort.h:16:6: note: void qsort<T>::quicksort(std::vector<T>&) [with T = int]
qsort.h:16:6: note: no known conversion for argument 1 from ‘std::vector<int, std::allocator<int> >*’ to ‘std::vector<int, std::allocator<int> >&’
部首:
template <class T>
class qsort
{
public:
void quicksort(vector<T> &v);
void qusort(vector<T> &v, int left, int right);
void print(vector<T> &v);
};
template <class T>
void qsort<T>::quicksort(vector<T> &v)
{
qusort(&v, 0, 0);
}
template <class T>
void qsort<T>::print(vector<T> &v)
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
}
主:
int main()
{
qsort<int> asort;
vector<int> v;
v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(3);
v.push_back(8);
v.push_back(4);
v.push_back(0);
v.push_back(9);
v.push_back(5);
v.push_back(6);
asort.quicksort(&v);
asort.print(&v);
return 0;
}
从主要函数调用(短版本)中删除&符号后更新的错误
qsort.h:在成员函数'void quisort :: qusort(std :: vector&amp;,int,int)[with T = int]'中: qsort.h:18:5:从'void quisort :: quicksort(std :: vector&amp;)[with T = int]'
实例化qsort.cc:22:22:从这里实例化 qsort.h:27:38:错误:从'int'无效转换为'const char *'[-fpermissive] /usr/include/c++/4.6/bits/basic_string.tcc:214:5:错误:初始化'std :: basic_string&lt; _CharT,_Traits,_Alloc&gt; :: basic_string(const _CharT *,const _Alloc&amp;)的参数1 [ with _CharT = char,_ Traits = std :: char_traits,_Alloc = std :: allocator]'[-fpermissive]
qsort.h:18:5:从'void quisort :: quicksort(std :: vector&amp;)[with T = int]'实例化 qsort.cc:22:22:从这里实例化 qsort.h:31:9:错误:'(&amp; v)中的'operator&lt;'不匹配 - &gt; std :: vector&lt; _Tp,_Alloc&gt; :: operator [] [with _Tp = int,_Alloc = std :: allocator,std :: vector&lt; _Tp,_Alloc&gt; :: reference = int&amp;,std :: vector&lt; _Tp,_Alloc&gt; :: size_type = long unsigned int]((long unsigned int)i))&lt;枢' qsort.h:31:9:注意:候选人是: /usr/include/c++/4.6/bits/stl_pair.h:207:5:注意:模板bool std :: operator&lt;(const std :: pair&lt; _T1,_T2&gt;&amp;,const std :: pair&lt; _T1, _T2&GT;&安培) /usr/include/c++/4.6/bits/stl_iterator.h:291:5:注意:模板bool std :: operator&lt;(const std :: reverse_iterator&lt; _Iterator&gt;&amp;,const std :: reverse_iterator&lt; _Iterator&gt;&amp; )
答案 0 :(得分:1)
您的成员函数通过引用获取参数。它们不接受指针(地址运算符&
返回的指针)。您只需传递对象,引用就会绑定:
asort.quicksort(v);
asort.print(v);