从一种类型转换为另一种类型的错误

时间:2012-03-06 11:40:28

标签: c++

我已经完成了桶排序的代码,这里是完整的代码

#include <iostream>
#include<iomanip>
using namespace std;
#define narray 8// array size;
#define  nbucket 5// bucket size;
#define interval 10// bucket range
struct node
{
int data;
struct node *next;
};
void BucketSort(int arr[]);
struct node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
void BucketSort(int arr[])
{

 int i,j;
 struct node **buckets;
 buckets = (struct node **)malloc(sizeof(struct node*) * nbucket); 
 for (i=0;i<nbucket;i++){
      buckets[i]=NULL;
 }
 for (int i=0;i<narray;i++){
  struct node *current;
  int pos=getBucketIndex(arr[i]);
  current=(struct node *)malloc(sizeof(struct node));
  current->data=arr[i];
  current->next=buckets[pos];
  buckets[pos]=current;


 }
 for (i=0;i<nbucket;i++){
    cout << "Bucket[" << i << "] : ";

     printBuckets(buckets[i]);
     cout<<endl;


 }
 for ( i=0;i<nbucket;i++){
   buckets[i]=InsertionSort(buckets[i]);



 }
 for (i=0;i<nbucket;i++){
     printBuckets(buckets[i]);
 cout<<endl;
 }
 //put  item back  to original array
 for (j=0,i=0;i<nbucket;i++){

struct  node * node;
node=buckets[i];
while(node){
    arr[j++]=node->data;
    node=node->next;


}


 }
//free memory
 for (i=0;i<nbucket;i++){
   struct node *node;
 node=buckets[i];
 while(node){

 struct node *temp;
  temp=node;
  node=node->next;
  free(temp);

 }


 }
 free(buckets);
 return ;


}

struct node *InsertionSort(struct node *list){

 struct node *k,*nodeList;
 if (list==0 || list->next==0){
 return list;
 }

 nodeList=list;
 k=list->next;
 nodeList->next;
 while(k!=0){
struct node *ptr;
if(nodeList->data>k->data){
   struct node *tmp;
   tmp=k;
   k=k->next;
   tmp->next=nodeList;
   nodeList=tmp;
   continue;



}
for (ptr=nodeList;ptr->next!=0;ptr=ptr->next){

    if (ptr->next->data>k->data)break;


}

if (ptr->next!=0){


struct node *temp;
temp=k;
k=k->next;
temp->next=ptr->next;
ptr->next=temp;
continue;



}
else{

    ptr->next=k;
    k=k->next;
    ptr->next->next=0;
    continue;


}


 }
  return nodeList;
}
int getBucketIndex(int value){

 return value/interval;
}
void print(int arr[]){
 int i;
 for(i=0;i<narray;++i){
cout<<setw(3)<<arr[i];
cout<<endl;


 }



}

void printBuckets(struct node *list){
  struct node *cur=list;
  while(cur){
      cout<<setw(3)<<cur->data;
      cur=cur->next;


  }


}
int main(){

int array[narray] = {29,25,3,49,9,37,21,43};

    cout << "Initial array" << endl;
    print(array);
    cout << "-------------" << endl;

    BucketSort(array); 
    cout << "-------------" << endl;
    cout << "Sorted array"  << endl;
    print(array); 





 return 0;
}

但它会写错误

Error   1   error C2664: 'printBuckets' : cannot convert parameter 1 from 'node *' to 'Node *'  c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp  39  bucket_sort
Error   2   error C2664: 'InsertionSort' : cannot convert parameter 1 from 'node *' to 'Node *' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp  45  bucket_sort
Error   3   error C2664: 'printBuckets' : cannot convert parameter 1 from 'node *' to 'Node *'  c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp  51  bucket_sort

我不明白为什么它不能转换?我已经指出了一切,所以出了什么问题?

2 个答案:

答案 0 :(得分:2)

C ++区分大小写,因此nodeNode是两种不同的类型,您只定义了类型node,但接受了完全不同的类型Node作为参数你的功能。编译器抱怨它不知道如何在这两种类型之间进行转换。

答案 1 :(得分:0)

typedef Node node;

将解决您的区分大小写问题。

虽然不是正确的修复