C++ 发射带有QT信号的int数组会导致数组未初始化
我在QT中声明了一个信号,如下所示:C++ 发射带有QT信号的int数组会导致数组未初始化,c++,arrays,qt,C++,Arrays,Qt,我在QT中声明了一个信号,如下所示: signals: void onResult(int,int[],int,int); 和一个插槽: public slots: void onBinarySearchComplete(int valueToSearchFor, int array[], int arraySize, int valueIndex); //this slot is connected to the onResult of the BinarySearch class.
signals:
void onResult(int,int[],int,int);
和一个插槽:
public slots:
void onBinarySearchComplete(int valueToSearchFor, int array[], int arraySize, int valueIndex); //this slot is connected to the onResult of the BinarySearch class.
我在MainWindow类中将数组声明为全局对象
int* m_array;
并在以后动态分配其内存
//create a random array to search.
int arraySize = 50;
m_array = new int[arraySize];
然后我将数组传递给BinarySearch对象,该对象也是全局声明的
//call the search
int returnVal= s->search(searchValue,m_array,arraySize); //the binarysearch class will emit the onResult signal when the search is complete.
在BinarySearch对象中,我对数组进行排序,然后在数组中搜索元素。
我可以在调用emit之前将数组打印到qDebug()
//this is the code that emits the signal in my BinarySearch class//
qDebug()<<"Printing Array Just Before Emit";
printArrayToDebug(array,arraySize);//the array is correct here!
emit onResult(valueToSearchFor,array,arraySize,halfWayIndex);
//这是在我的BinarySearch类中发出信号的代码//
我喜欢咖喱;为什么你会使用旧的C数组(当参数通过时,它们会衰减为指针,不知道它们的大小和大小不能被调整)当你可以使用<代码> STD::向量< /代码>?我在90年底在C学院学习C,在2003左右切换到C ^ -所以我从来没有真正研究过C++。我看过std::vector,但在这里使用int数组对我来说似乎是一个有效的用法。我的意思是,我在C#中就是这样做的,因为它实际上是一个整数数组,一旦应用程序启动,它的大小不会改变。人们不再使用int[]了吗?我在你的声明中没有看到任何问题,只有你用C风格做的,而且它是有效的。我有一个疑问:我看到它声明了m_array
变量并对其进行了初始化,但是当您发出信号发送array
变量时,您可以显示如何创建变量数组。
//This is the slot the receives the signal in my MainWindow class//
void MainWindow::onBinarySearchComplete(int valueToSearchFor, int array[], int arraySize, int valueIndex){
//this array appears uninitialized!
for(int i=0;i<arraySize;i++){
qDebug()<<array[i];
}