错误 LNK2001:未解析的外部符号"int * array"
error LNK2001: unresolved external symbol "int * array"
当我尝试编译它时,我有这个错误,我真的不知道该怎么做。也许你能帮我解决这个问题。
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>——构建开始:项目:practical2配置:调试Win32 - 1>编译…1> quicksort.cpp 1>链接…1>快速排序。obj:错误LNK2001:未解决的外部符号"int * array" (?array@@3PAHA) 1>C:UsersAmedDocumentsVisual Studio 2008Projectspractical2Debugpractical2.exe:致命错误LNK1120: 1未解决的外部1>构建日志保存在"file://C:UsersAmedDocumentsVisual Studio 2008Projectspractical2practical2DebugBuildLog.htm" 1>practical2 - 2错误(s), 0警告(s)========== 构建:0成功,1失败,最新的,0跳过 ==========
您在quicksort.cpp
中写了extern int array[]
,但实际上没有在任何地方定义它。有一个未解析的符号,因为它不存在,所以链接器找不到它来解析它
您可以通过将外部更改为:int数组[1024];
指出:任意选择1024作为数组维度。选择静态定义的c风格数组的维数有时很棘手。你可以通过切换到像标准库的std::vector类这样的动态结构来避免这个问题:std::向量nameOtherThanArray;//;)
将(全局)变量命名为"array"可能太过遗传,无法实现预期的用途,而且可能与其他库中的名称冲突。我没有尝试对其余部分进行代码审查,但如果我正在编写一个真正的通用方法,我希望传入正在操作的结构,或者如果它只是一个临时帮助器,则在本地声明它。当然,就像我说的,我没有仔细检查剩下的代码,所以您的方法可能出于其他原因而有意义。
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 是否可以将无符号 int 的最大值转换为 int 并将结果转换为 -1?
- 为什么QByteArray的大小是"int"而不是"无符号int"
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- 使用无符号int作为二进制来在c++中实现一个集
- C++如何将变量传递给带有无符号 int & 的参数uint16_t函数?
- 为什么我不能使用在 Visual C++ 32 位中实现运算符无符号 int() 作为数组索引的类?
- 视觉C++转换和写入值为 10 的无符号 int 给出 5 个字节
- uint8_t在转换为无符号 int 时未打印正确的值
- G++ 错误:重载的"abs(无符号 int)"的调用不明确
- 将 3D 矢量浮点打包到无符号 int 中并将其解压缩
- 如何使用 JNI 将 double 和无符号 int 从本机 c 库返回到 java
- 为什么printf和cout为此无符号int提供了不同的输出
- memcpy 从无符号字符 * 到无符号 int
- C++ 重载与有符号和无符号 int 不同
- 创建具有未定义溢出的无符号 int
- 将大双精度转换为无符号 int 期间堆栈损坏
- 错误:成员引用基类型"uint32_t"(也称为"无符号 INT")不是结构或联合
- 无符号int的比较始终是正确的(NPOS问题?)