来自 Scala 的 C 语言动态调度



我正在尝试用 C 语言实现动态调度,从 scala 翻译。 作为我在 C 语言中的代码的一部分,我有

typedef struct File{
  void (**vtable)();
  char *node;
  int *size;
}File;
//function
Node  *newFile(char *n, int *s);
int *newFile_Size(Node* n){
  return (int *)n->size;
}
void (*Folder_Vtable[])() = {(VF) &newFile_Size};
Node  *newFile(char *n, int *s){
  File *temp = NEW(File);
  temp->vtable= Folder_Vtable;
  temp->node=n;
  temp->size=s;
  return (Node *) temp;
}

这是以下代码在 scala 中的翻译:

class File(n: String, s: Int) extends Node(n) {
  var size: Int = s
}

当我编译我的 C 代码时,我收到此错误:

./solution.c:123:30: note: passing argument to parameter 's' here
Node  *newFile(char *n, int *s){

函数的调用方式如下:

Node* file = newFile("file_to_test", 1);

我收到此警告/错误 5 次。有人可以向我解释我在这里做错了什么吗?

好的,

所以这是问题所在:

在您的主要:

Node* file1 = newFile("file_to_test", 1);

newFile()需要对整数的引用,但您直接传递整数。

您应该尝试以下操作:

int size = 1;
Node* file1 = newFile("file_to_test", &size);

或者(如果您不想修改您的主):

typedef struct File{
  void (**vtable)();
  char *node;
  int size;
}File;
//function
Node  *newFile(char *n, int s);
// Update other functions

最新更新