C++ “不确定如何修复”;成员参考资料“;错误
我的main.cpp文件中有问题,程序告诉我,C++ “不确定如何修复”;成员参考资料“;错误,c++,C++,我的main.cpp文件中有问题,程序告诉我,成员引用基类型“int[11]”不是我的快速排序行和for循环的结构或联合。然后在cout行中显示向字符串添加'int'不会附加到字符串,并且“使用数组索引使此警告静音” 下面是我遇到问题的main.cpp文件 #include <iostream> #include "QuickSort.h" using namespace std; int main() { int F[] = {12, 2, 16, 30, 8, 28,
成员引用基类型“int[11]”不是我的快速排序行和for循环的结构或联合。然后在cout行中显示向字符串添加'int'不会附加到字符串,并且“使用数组索引使此警告静音”
下面是我遇到问题的main.cpp文件
#include <iostream>
#include "QuickSort.h"
using namespace std;
int main() {
int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
QuickSort(F, 0, F.length-1);
for (int i = 0; i<F.length; i++){
cout << F[i] + " ";
}
return 0;
}
下面是我的QuickSort.cpp文件:
QuickSortRecursion::QuickSortRecursion(){
return;
}
int QuickSortRecursion::Partition(int a[], int low, int high){
int pivot = high;
int i = low;
int j = high;
while (i<j){
if (a[i] <= a[pivot]){
i++;
}if (a[i] > a[pivot]){
if ((a[i] > a[pivot]) && (a[j] <= a[pivot])){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
} if (a[j] > a[pivot]){
j--;
}
}
}
int temp = a[i];
a[i] = a[pivot];
a[pivot] = temp;
return i;
}
void QuickSortRecursion::QuickSort(int a[], int low, int high){
if (low >= high){
return;
}
int split = Partition (a, low, high);
QuickSort(a, low, split-1);
QuickSort(a, split+1, high);
}
QuickSortRecursion::QuickSortRecursion(){
返回;
}
int QuickSortRecursion::Partition(inta[],intlow,inthigh){
int轴=高;
int i=低;
int j=高;
而(i a[pivot])&(a[j]a[pivot]){
j--;
}
}
}
int temp=a[i];
a[i]=a[pivot];
a[枢轴]=温度;
返回i;
}
void QuickSort递归::快速排序(int a[],int低,int高){
如果(低>=高){
返回;
}
int split=分区(a、低、高);
快速排序(a、低、拆分-1);
快速排序(a、拆分+1、高);
}
中的
使用std::vector
代替原始数组。std::array更合适,但我没有一个好的公式
std::array<int> F = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
^
Need to specify array size here, and if you knew that you there
wouldn't be a problem.
使用sizeof
获取长度(以字节为单位),然后将其除以sizeof
元素以获取数组中的元素数
int main() {
int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
const int length = sizeof(F) / sizeof(F[0]);
QuickSort(F, 0, length -1);
for (int i = 0; i<length ; i++){
cout << F[i] + " ";
}
return 0;
}
intmain(){
intf[]={12,2,16,30,8,28,4,10,20,6,18};
常量int length=sizeof(F)/sizeof(F[0]);
快速排序(F,0,长度-1);
对于(inti=0;iSide注意:您的分区看起来不太正确。您有一些看起来像a的东西,但不需要任何看起来像if((a[i]>a[pivot])&&&(a[j]
int main() {
int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
QuickSort(F, 0, std::size(F)-1);
for (int i = 0; i<std::size(F); i++){
cout << F[i] + " ";
}
return 0;
}
std::array<int> F = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
^
Need to specify array size here, and if you knew that you there
wouldn't be a problem.
int main() {
std::vector<int> F = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
QuickSort(F.data(), 0, F.size()-1);
for (int i = 0; i<F.size(); i++){
cout << F[i] + " ";
}
return 0;
}
int main() {
int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
const int length = sizeof(F) / sizeof(F[0]);
QuickSort(F, 0, length -1);
for (int i = 0; i<length ; i++){
cout << F[i] + " ";
}
return 0;
}