Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从一种类型转换为另一种类型时出错_C++ - Fatal编程技术网

C++ 从一种类型转换为另一种类型时出错

C++ 从一种类型转换为另一种类型时出错,c++,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 Bucket

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

#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;
}

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

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

typedef节点

将解决您的案例敏感性问题


虽然不是正确的修复方法

哦,天哪,不是另一个使用iostream的C
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