[英]why am I getting this error for size() of arrays and how do I declare an array of boost::threads?
我收到此错误。 该功能应该执行mergeSort,但最后没有进行合并。 稍后我会做,但是我想确保它不会在最后进行最终合并。
编辑:我必须使用#include boost / thread.hpp,因为我学校使用的Linux服务器没有c ++ 11
multi.cpp: In function 'int main()':
multi.cpp:116:29: error: request for member 'size' in 'data', which is of non-class type 'int [1000000]'
multi.cpp:116:54: error: request for member 'size' in 'data', which is of non-class type 'int [1000000]'
multi.cpp:117:32: error: expected primary-expression before 't1'
multi.cpp:117:32: error: expected ';' before 't1'
multi.cpp:120:31: error: request for member 'size' in 'data', which is of non-class type 'int [1000000]'
这是我的代码。 我很确定我的错误是由于我不知道如何设置线程数组而引起的。 我可以等一个线程完成后再向阵列添加另一个线程吗?
#include <cstdlib>
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void merge(int arr[], int l, int m, int r);
void mergeSort(int arr[], int l, int r);
bool is_sorted(int* data, int size);
bool is_sorted(int* data, int size) {
for(int i = 0; i < size - 1; ++i)
if(data[i] > data[i+1])
return false;
return true;
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
struct my_sort{
public:
void operator()()
{
mergeSort(arr, l, r);
}
my_sort(){}
my_sort(int array[], int left, int right)
{
arr = array;
l = left;
r = right;
}
private:
int* arr;
int l;
int r;
};
int main() {
int data[1000000];
for(int i = 0; i < 1000000; i++)
data[i] = rand();
unsigned threads = boost::thread::hardware_concurrency();
boost::thread threading[threads];
for(int i = 0; i < threads; i++)
{
my_sort m1(data, i * data.size()/threads, i * data.size()/threads - 1);
threading[i] = boost::thread t1{m1};
}
cout << is_sorted(data, data.size()-1);
return 0;
}
multi.cpp:116:29: error: request for member 'size' in 'data', which is of non-class type 'int [1000000]'
...
multi.cpp:120:31: error: request for member 'size' in 'data', which is of non-class type 'int [1000000]'
类型int[1000000]
没有size
方法。 它是原始类型。 使用sizeof
或更好的std::vector
。 (当您可以访问C ++ 11时,可以使用std::array
。)
multi.cpp:117:32: error: expected primary-expression before 't1'
multi.cpp:117:32: error: expected ';' before 't1'
...
threading[i] = boost::thread t1{m1};
这是语法错误。 您是否要在表达式中定义变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.