سؤال

I am trying to implement dynamic dispatch in C, translating from scala. as part of my code in C I have

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

which is the translation of below code in scala:

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

when I compile my C code I get this error:

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

This is how the function is called:

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

and I get this warning/error like 5 times. Can someone please explain to me what I am doing wrong here?

هل كانت مفيدة؟

المحلول

Ok so here is the issue:

In your main:

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

newFile() is expecting a reference on integer but you are passing an integer directly.

You should try something like:

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

Or (if you don't want to modify your main):

typedef struct File{
  void (**vtable)();
  char *node;
  int size;
}File;
//function
Node  *newFile(char *n, int s);
// Update other functions
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top