visual studio中出现错误.它在CodeBlocks中工作.请求const
Error in visual studio. It works in CodeBlocks... Asking for const
#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++中,可变大小的数组是不允许的,应该总是有一个固定的大小,在编译的时候就知道了。
根据您想要实现的目标,解决方案将不同。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- CodeBlocks Qt HelloWorld.exe已停止工作(C++)
- filename.exe已停止在CodeBlocks中工作
- C++:在CodeBlocks上编译和运行的程序可以工作,但从终端它给出错误语句
- visual studio中出现错误.它在CodeBlocks中工作.请求const
- CodeBlocks SFML音频库将无法工作