visual studio中出现错误.它在CodeBlocks中工作.请求const

Error in visual studio. It works in CodeBlocks... Asking for const

本文关键字:CodeBlocks 工作 请求 它在 const 错误 studio visual      更新时间:2023-10-16
#include <iostream>     // used for input and output.
#include <conio.h>      // used for getch().
#include <cstdlib>      // used for random number generation.
#include <time.h>       // used to seed the random number generator.
#include <cmath>        // used for floor
using namespace std;

void array_merger(int* array, int beg, int endd)
{
int median = floor((beg + endd) / 2); // midpoint
int min_pos = 0;                      // minimum index of the array
int new_beg = beg;                    // new beginning of the array index used to cycle through the array from left to right
int new_end = median + 1;             // indexed at the midpoint of the array
// Temp array
int temp[endd-beg+1];                 //declares and initializes a new temporary array to hold the unsorted data

while ( new_beg <= median && new_end <= endd )       // merges in sorted form the 2 arrays
    if ( array[new_beg] < array[new_end] )
        temp[min_pos++] = array[new_beg++];
    else
        temp[min_pos++] = array[new_end++];

while ( new_beg <= median )                          // merges the remaining elements in left array
    temp[min_pos++] = array[new_beg++];

while ( new_end <= endd )                            // merges the remaining elements in right array
    temp[min_pos++] = array[new_end++];

for ( int i = beg; i <= endd; i++ )                  // moves from temp array to master array
    array[i] = temp[i-beg];
}

void merge_sort(int* array, int beg, int endd)
{
if ( beg < endd ) // checks to see if there is more than one element in the array
{
    int median = floor((beg + endd) / 2);  // finds the midpoint of the array of 2 or more elements
    merge_sort(array, beg, median);        // uses recursion to further split the array in half (divide and conquer) on the left side
    merge_sort(array, median + 1, endd);   // now working back up from left to right
    array_merger(array, beg, endd);        // calls array_merger to put the pieces back together
}
}
// printer function
void print(int *array, int ARRAY_LENGTH) //prints results
{
for ( int i = 0; i < ARRAY_LENGTH; i++ ) // cycles through printing each element array from index 0 to the last index of the array
    cout << array[i] << " ";
cout << endl;
}
//reusing your randomizeArray function
void randomizeArray(int array[], int length)
{
srand ( (unsigned) time(NULL));         // seed random number generator.
int i = 0;
do
{
    array[i] = rand() % 100000 + 1;     // a random number in the range of 1 to 100,000 is assigned.
    i++;
} while (i < length);
}

void timeSort(int array[], int length)
{
clock_t startTime, endTime;
// Randomize values in array.
randomizeArray(array, length);
// Time array sort.
startTime = clock();            // Get starting time.
merge_sort(array, 0, length);   // Sort array.
endTime = clock();              // Get ending time.
// Display algorithm's running time as difference between starting and ending time.
cout    << "Merge sort time for an array of "
        << length
        << ": "
        << ( (float) endTime - (float) startTime) / CLOCKS_PER_SEC * 1000
        << " milliseconds." // On my machine, CLOCKS_PER_SEC is equal to 1000 and
                            // thus milliseconds is the correct unit.
        << endl;
}

int main()
{
//const int ARRAY_LENGTH = 20000; //sets the array length for timeSort (bigO)
//int test1[ARRAY_LENGTH]; // initializing an array for the first test
//timeSort(test1, ARRAY_LENGTH); // calling timeSort
//int test2[ARRAY_LENGTH*2];
//timeSort(test2, ARRAY_LENGTH*2);
//int test3[ARRAY_LENGTH*3];
//timeSort(test3, ARRAY_LENGTH*3);
//int test4[ARRAY_LENGTH*4];
//timeSort(test4, ARRAY_LENGTH*4);

const int ARRAY_LENGTH = 20;  // tests to see if the logic is correct
int logic_test[ARRAY_LENGTH];
randomizeArray(logic_test, ARRAY_LENGTH);
cout << "Input: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print unsorted array
merge_sort(logic_test, 0, ARRAY_LENGTH-1);
cout << "Output: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print merge sorted array

getch();
return 0;
}

好吧. .所以我得到的错误是在array_merge函数中。它在初始化int temp[end -beg+1]时要求常量。这将导致它无法执行该方法的其余部分。

同样,int median = floor((beg+endd)/2)也要求常量。

这些东西在代码块中不会引起问题,但在Visual Studio中就会。

原因一定是编译器的不同。最有可能的是,您的CodeBlocks正在使用c++编译器的扩展,并且允许可变大小的数组。VisualStudio运行的是严格的c++编译器,在c++中,可变大小的数组是不允许的,应该总是有一个固定的大小,在编译的时候就知道了。

根据您想要实现的目标,解决方案将不同。