当我尝试编译它时,我有这个错误,我真的不知道该怎么做。 也许你可以帮我解决这个问题。
Main.cpp的:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <list>
using namespace std;
list<int> liste;
int size=0;
void list_initialization(){
cout << "Quicksort" << endl << endl;
cout <<"List Initialization" << endl;
while ((size<1)||size>100){
cout << "Please enter the size (between 1 and 100)" << endl;
cin >> size;
}
srand ( time(NULL) );
for (int i=0; i<size; i++){
liste.push_back(rand() %100 +1);
}
}
void list_display(){
cout << endl;
for (list<int>::const_iterator ite=liste.begin(), end=liste.end(); ite!=end; ++ite)
cout << *ite << " ";
cout << endl;
}
int choose_pivot( int const& left, int const& right){
int pivot;
pivot = rand()%(right-left) + left ;
return pivot;
}
int partition(int left, int right, int pivotIndex){
//We Save pivotValue
list<int>::iterator itPivotIndex = liste.begin();
advance (itPivotIndex, pivotIndex);
int pivotValue = *itPivotIndex;
//Those 2 iterators will be used to swap 2 elements
list<int>::iterator itSwap1 = liste.begin();
list<int>::iterator itSwap2 = liste.begin();
//2 iterators to point left and right elements
list<int>::iterator itLeft = liste.begin();
advance (itLeft, left);
list<int>::iterator itRight = liste.begin();
advance (itRight, right);
//1 iterator to point the StoreIndex
list<int>::iterator itStoreIndex=itLeft;
//Move Pivot to End
advance(itSwap1, pivotIndex);
advance(itSwap2, right);
swap(*itSwap1, *itSwap2);
//Move all elements less than pivotValue before the pivot
for(list<int>::iterator it=itLeft; it!=itRight; it++)
if (*it < pivotValue){
//Swap array[k] and array[storeIndex]
itSwap1=it;
itSwap2=itStoreIndex;
swap(*itSwap1, *itSwap2);
itStoreIndex++;
}
//Move pivot to its final place
swap(*itStoreIndex, *itRight);
return (distance(liste.begin(), itStoreIndex));
}
void quicksort (int left, int right){
int pivotNewIndex=0;
list<int>::iterator ite=liste.begin();
if (left < right){
int pivotIndex = choose_pivot(left,right);
advance (ite,pivotIndex);
cout << "The pivot is " << *ite <<endl;
pivotNewIndex = partition(left, right, pivotIndex);
list_display();
cout << endl;
// Recursively sort elements smaller than the pivot
quicksort(left, pivotNewIndex - 1);
// Recursively sort elements at least as big as the pivot
quicksort(pivotNewIndex + 1, right);
}
}
int main()
{
list_initialization();
list_display();
cout << endl;
int left=0;
int right=size-1;
quicksort(left, right);
cout << "Sorted List :";
list_display();
cout << endl;
return 0;
}
quicksort.cpp:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <list>
using namespace std;
extern int array [];
int selectPivot( int const& left, int const& right);
int Partition(int left, int right, int pivotIndex);
void sorting (int left, int right);
int selectPivot( int const& left, int const& right){
int pivot;
pivot = rand()%(right-left) + left ;
return pivot;
}
int Partition(int left, int right, int pivotIndex){
int temp=0;
int storeIndex=left;
int pivotValue = array[pivotIndex];
temp=pivotValue;
pivotValue=array[right];
array[right]=temp;
for (int k=left; k<right; k++)
if (array[k] < pivotValue){
temp=array[k];
array[k]=array[storeIndex];
array[storeIndex]=temp;
storeIndex++;
}
temp = array[storeIndex];
array[storeIndex]=array[right];
array[right]=temp;
return storeIndex;
}
void sorting (int left, int right){
int pivotNewIndex=0;
if (left < right){
int pivotIndex = selectPivot(left,right);
pivotNewIndex = Partition(left, right, pivotIndex);
sorting(left, pivotNewIndex - 1);
sorting(pivotNewIndex + 1, right);
}
}
-------------- 1&gt; ------构建开始:项目:实用2,配置:调试Win32 ------ 1&gt;编译... 1&gt; quicksort.cpp 1&gt;链接... 1&gt; quicksort.obj:错误LNK2001:未解析的外部符号“int * array”(?array @@ 3PAHA)1&gt; C:\ Users \ Amed \ Documents \ Visual Studio 2008 \ Projects \ practical2 \ Debug \ practical2.exe:致命错误LNK1120:1未解析的外部1&gt;构建日志保存在“file:// c:\ Users \ Amed \ Documents \ Visual Studio 2008 \ Projects \ practical2 \ practical2 \ Debug \ BuildLog”。 htm“1&gt; practical2 - 2错误,0警告 ==========构建:0成功,1个失败,0个最新,0个跳过==========
答案 0 :(得分:4)
您在extern int array[]
中写了quicksort.cpp
,但实际上并没有在任何地方定义它。有一个未解析的符号,因为它不存在于任何地方,因此链接器无法找到它来解析它。
答案 1 :(得分:0)
您可以修改未解析的外部,方法是将其更改为: int array [1024];
注意: 任意选择1024作为阵列维度。选择静态定义的C风格数组的维度有时很棘手。 您可以通过切换到标准库的std :: vector类之类的动态结构来避免此问题: std :: vector nameOtherThanArray; //;)
命名(全局)变量&#34;数组&#34;除了可能与其他libs中的名字发生冲突外,它可能过于遗传而无法传授预期的用途。我没有尝试对其余部分进行编码检查,但如果我正在编写一个真正的通用方法,我想传递被操纵的结构,或者如果它只是一个临时帮助器,则在本地声明它。当然,就像我说的那样,我没有仔细检查其余的代码,所以你的方式可能有其他原因。