Reputation: 3949
I am using the following scala code trying to translate this code into C using virtual method tables(dynamic dispatch).
this is the code in scala:
abstract class Node(n: String) {
val id = Node.next
var name: String = n
def size: Int
def nrNodes: Int = 1
def find(q: String): Set[Node] =
if(name.contains(q)) Set(this) else Set()
}
My problem is with this part of the code:
def find(q: String): Set[Node] =
if(name.contains(q)) Set(this) else Set()
I am trying to translate it into C, and this is what I have so far:
Set find(Node *n, char * s){
if(strstr(s,n->name)!=0){
return (Set) n->name;
}
return ((Set (*)(Node *)) n->vtable[FIND])(n);
}
So find returns a set of nodes if it contains a Node else an empty set. when I run this it gives the following error:
error: unknown type name 'Set'
use of undeclared identifier 'Set'
I am not sure if I have to use struct Set or my find method is wrong in general!
Here is my vtable:
enum Node_vtablekeys{
SIZE=0,
NRNODERS=1,
FIND=2
};
Upvotes: 0
Views: 259
Reputation: 3496
As said in the comments it seems that you forgot to declare your structure:
typedef struct _set
{
// Whatever Set must contains
} Set;
Moreover, I don't really know Scala but in find()
it looks like if you don't find a match, the function actually creates a new Set
so I guess there should be a malloc
in your function.
Can you show us the scala Set structure ?
Depending of how you use find()
it might be more useful to return a reference rather than the Set
structure itself.
Set * find(Node *n, char * s)
{
if(strstr(s,n->name)!=0)
{
return (Set) &(n->name);
}
else
{
Set * new_set = malloc(sizof(Set));
// Copy attributes from n->vtable[FIND])(n) to new_set
return new_set;
}
}
Upvotes: 1