C++ 我能';我不知道是什么';我的密码有问题吗

C++ 我能';我不知道是什么';我的密码有问题吗,c++,C++,我正在尝试做一个冒泡排序算法,但我一直遇到错误。我的第一个for循环经常出错,它声称它是一个非限定id。我的一个变量需要在for循环中再次声明一个类型,这会导致整个代码出现问题 bubble-sort.cpp:12:1: error: expected unqualified-id before ‘for’ 12 | for (int hop=0; hop <= 10; hop++){ | ^~~ bubble-sort.cpp:12:17: error: ‘hop’ d

我正在尝试做一个冒泡排序算法,但我一直遇到错误。我的第一个for循环经常出错,它声称它是一个非限定id。我的一个变量需要在for循环中再次声明一个类型,这会导致整个代码出现问题

bubble-sort.cpp:12:1: error: expected unqualified-id before ‘for’
   12 | for (int hop=0; hop <= 10; hop++){
      | ^~~
bubble-sort.cpp:12:17: error: ‘hop’ does not name a type
   12 | for (int hop=0; hop <= 10; hop++){
      |                 ^~~
bubble-sort.cpp:12:28: error: ‘hop’ does not name a type
   12 | for (int hop=0; hop <= 10; hop++){
      |                            ^~~
bubble-sort.cpp:28:1: error: expected declaration before ‘}’ token
   28 | };
      | ^
bubble sort.cpp:12:1:错误:在“for”之前应为非限定id

12 |对于(int-hop=0;hop可以帮助您改进原始代码的注释:

// avoid making global variables. Only constants that are used throughout your program should be made global.
//Also when making an array like this instead of using 5 do the following
// const int SIZE = 5;
// int array[SIZE] = {19,3,90,1,9};
// this way you can use the size later on
int array [5] = {19,3,90,1,9};

class BubSor {

public:
// you have to put this in a function
    int temp;
// bounding it by 10 is not correct because this number will change
// and you also the most passes you will ever have to make will be the size of the array
    for (int hop=0; hop <= 10; hop++){
// you do not need this while loop
// also hope should be hope
        while (hope >=10 ){
// this should be bound by the size of the array -1, because you are comparing to j + 1, otherwise you will go over the size of the array
            for (int j=0; j <=5; j++;){
                if (array[j]>array[j+1]){
                // here you should place it into temp instead
                    array [j] = temp;
                    // this should be done in reverse order
                    array [j+1] = array [j];
                    // swap the next line too
                    temp = array [j+1];
                }
            }
        }
    }
}; // you do not need the extra ; here
}; // this one is necessary because it is a class

int main (){
    BubSor object;
// here you need to call the function that is inside the class
    object  ;

    return 0;
}

//避免使用全局变量。只有在整个程序中使用的常量才应使用全局变量。
//另外,当创建这样的数组而不是使用5时,请执行以下操作
//常数int SIZE=5;
//int数组[SIZE]={19,3,90,1,9};
//这样以后就可以使用该尺寸了
int数组[5]={19,3,90,1,9};
巴伯索级{
公众:
//你必须把它放在一个函数中
内部温度;
//将其限定为10是不正确的,因为此数字将更改
//而且你需要做的最多的传递就是数组的大小
对于(int-hop=0;hop=10){
//这应该受到数组大小-1的限制,因为您将与j+1进行比较,否则将忽略数组的大小
对于(int j=0;j数组[j+1]){
//在这里,你应该把它放在临时代替
数组[j]=温度;
//这应该按相反的顺序进行
数组[j+1]=数组[j];
//也换下一行
温度=阵列[j+1];
}
}
}
}
};//你不需要额外的;给你
};//这个是必需的,因为它是一个类
int main(){
布布索物体;
//这里需要调用类内部的函数
对象
返回0;
}
上述建议的可能实施版本:


// no global variable array

class BubSor {

public:
// the code is wrapped in a reusable function called sort which takes
// as parameters an array and its size N
    void sort(int* array, const int N)
    {
        int temp;
        bool swapped = true; // if you haven't made any swaps then the array is already sorted
        // bound the for loop by the actual size of the array
        for (int hop = 0; hop < N && swapped; hop++) {
            swapped = false;
            // inner for loop goes only to the size -1 because
            // we are comparing j to j + 1, otherwise we go over the size
            // of the array
            for (int j = 0; j < N-1; j++) {
                // if unordered swap
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                    swapped = true;
                }
            }
        }
    }
};

int main() {

    // good practice to place it in a const
    const int N = 13;
    int arr[N] = { 19,3,1,2,3,11,10,9,3,4,90,1,9 };
    BubSor object;
    // use the sort function to sort the array
    object.sort(arr, N);
    // print the array to see if the function is working
    for (int x : arr)
    {
        cout << x << " ";
    }
    cout << "\n";
}

//无全局变量数组
巴伯索级{
公众:
//代码被包装在一个名为sort的可重用函数中,该函数采用
//作为参数的数组及其大小N
无效排序(int*数组,常量int N)
{
内部温度;
bool swapped=true;//如果尚未进行任何交换,则数组已排序
//按数组的实际大小绑定for循环
对于(int-hop=0;hop数组[j+1]){
温度=阵列[j+1];
数组[j+1]=数组[j];
数组[j]=温度;
交换=真;
}
}
}
}
};
int main(){
//将其放置在容器中的良好实践
常数int N=13;
int arr[N]={19,3,1,2,3,11,10,9,3,4,90,1,9};
布布索物体;
//使用sort函数对数组进行排序
object.sort(arr,N);
//打印数组以查看函数是否正常工作
用于(整数x:arr)
{

CUT不能有语句,例如循环的代码>函数外。在类定义的中间有一个;没有意义,也不会编译。如果你学会正确识别代码,你会发现代码更容易阅读和理解。这使得理解组织和逻辑变得更容易。@ IgorTandetn。ik这不是唯一的问题。
hope
从未被声明过,即使它应该是
hop
,但
while
循环只会在其父循环的最后一次循环中执行。我遵循。它比其他编码样式稍大一些,但遵循它几乎可以消除所有编译器错误和bug。什么无论你用什么方法,关键是要有规律。当被命令包围时,很多错误都会像疼痛的拇指一样突出。难怪佩奇要下台了;我记得有一段时间,人们可以准确地找到数千个工作冒泡排序示例,只需几下键盘就可以从中收集知识。非常感谢我意识到我现在是多么愚蠢,我完成了我忘了类的用途了,我把它们错当成函数了。我真的很感激你对我的代码所做的评论。这真的会帮助我改进代码结构。再次感谢!没问题。我很高兴能帮助你。

// no global variable array

class BubSor {

public:
// the code is wrapped in a reusable function called sort which takes
// as parameters an array and its size N
    void sort(int* array, const int N)
    {
        int temp;
        bool swapped = true; // if you haven't made any swaps then the array is already sorted
        // bound the for loop by the actual size of the array
        for (int hop = 0; hop < N && swapped; hop++) {
            swapped = false;
            // inner for loop goes only to the size -1 because
            // we are comparing j to j + 1, otherwise we go over the size
            // of the array
            for (int j = 0; j < N-1; j++) {
                // if unordered swap
                if (array[j] > array[j + 1]) {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                    swapped = true;
                }
            }
        }
    }
};

int main() {

    // good practice to place it in a const
    const int N = 13;
    int arr[N] = { 19,3,1,2,3,11,10,9,3,4,90,1,9 };
    BubSor object;
    // use the sort function to sort the array
    object.sort(arr, N);
    // print the array to see if the function is working
    for (int x : arr)
    {
        cout << x << " ";
    }
    cout << "\n";
}