从 int* 到 int& 的转换

Conversion from int* to int&

本文关键字:int 转换      更新时间:2023-10-16

我一直在尝试编译并使用&符号,但仍然无法找出错误所在。什么好主意吗?

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&, int, int) [with T = int] ':qsort.h:18:5:实例化自' void quisort::quicksort(std::vector&) [with T = int] '

qsort。Cc:22:22:从这里实例化Qsort.h:27:38:错误:从' int '到' const char* '的转换无效[-fpermissive]/usr/include/c + +/4.6/位/basic_string。tcc:214:5:错误:初始化参数1的' std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator] ' [-fpermissive]

qsort.h:18:5: instantiated from ' void quisort::quicksort(std::vector&) [with T = int] 'qsort。Cc:22:22:从这里实例化Qsort.h:31:9:错误:没有匹配' operator<' in ' (&v)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator, std::vector<_Tp, _Alloc>::reference = int&, std::vector<_Tp, _Alloc>::size_type = long unsigned int](((long unsigned int)i)) &, const std::pair<_T1, _T2>&)/usr/include/c++/4.6/bits/stl_iterator.h:291:5:注:template bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)

成员函数通过引用接受实参。它们不接受指针(地址操作符&返回的指针)。您只需要传递对象,引用将绑定:

asort.quicksort(v);
asort.print(v);