我正在实现一个简单的merge
函数,但我被卡住了,因为编译器给了我无法解释的错误。这是我的merge
功能:
void merge(void *a, int beg, int middle, int end, int (*cmp)(const void*, const void*
{
std::stack<void*> first;
std::stack<void*> second;
for(int i = beg; i < middle; i++) {
first.push(a+i);
}
for(int i = middle; i < end; i++) {
second.push(a+i);
}
for(int i = beg; i < end; i++) {
if(first.empty()) {
void *tmp = second.top();
second.pop();
a+i = tmp;
} else if(second.empty()) {
void *tmp = first.top();
first.pop();
a+i = tmp;
} else if(cmp(first.top(), second.top())) {
void *tmp = first.top();
first.pop();
a+i = tmp;
} else {
void *tmp = second.top();
second.pop();
a+i = tmp;
}
}
}
这是错误:
sort.h: In function `void merge(void*, int, int, int, int (*)(const void*, const void*))':
sort.h:9: error: pointer of type `void *' used in arithmetic
sort.h:12: error: pointer of type `void *' used in arithmetic
sort.h:19: error: pointer of type `void *' used in arithmetic
sort.h:19: error: non-lvalue in assignment
sort.h:23: error: pointer of type `void *' used in arithmetic
sort.h:23: error: non-lvalue in assignment
sort.h:27: error: pointer of type `void *' used in arithmetic
sort.h:27: error: non-lvalue in assignment
sort.h:31: error: pointer of type `void *' used in arithmetic
sort.h:31: error: non-lvalue in assignment
谁能帮我?TIA。