从 int* 到 int& 的转换
Conversion from int* to int&
我一直在尝试编译并使用&符号,但仍然无法找出错误所在。什么好主意吗?
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);
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 将字符串中的 int 转换为字母;
- 主.cpp:18:20:错误:从"int*"转换为"int"会失去精度 [-fa
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- 如何将矢量<int>转换为字符数组?
- C++ 无法从 const int* 转换为 const_iterator
- 我不断收到错误 C2440'=':无法从"int *(__cdecl *)(int *,int *,int,int)"转换为"int *
- 如何将 int[4] 转换为 std::array<int,4>?
- 如何将data[i].int转换为vaible
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 将私有矩阵形式 int 转换为双 C++
- 无法将参数 1 从 'int' 转换为 'int &'
- 将 int 转换为字符串后始终得到 0
- isspace 函数的性能警告,从 int 转换为布尔值
- 将大 int 转换为浮点数,而不舍入 c++
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将未签名的INT转换为BCD
- 在 C++ 中将 int 转换为双精度