Just for s & g. I wanted to build my own library in C. I wanted to make it follow the C# object notion and realized that the only way to do so is to have the base types use pointers to functions as their members.
Well, I am stuck and have no clue why. The following is a sample of what a String base type may look like:
#ifndef STRING_H
#define STRING_H
typedef struct _string
{
char* Value;
int Length;
String* (*Trim)(String*, char);
} String;
String* String_Allocate(char* s);
String* Trim(String* s, char trimCharacter);
#endif /* STRING_H */
And the implementation:
String* Trim(String* s, char trimCharacter)
{
int i=0;
for(i=0; i<s->Length; i++)
{
if( s->Value[i] == trimCharacter )
{
char* newValue = (char *)malloc(sizeof(char) * (s->Length - 1));
int j=1;
for(j=1; j<s->Length; j++)
{
newValue[j] = s->Value[j];
}
s->Value = newValue;
}
else
{
break;
}
}
s->Length = strlen(s->Value);
return s;
}
String* String_Allocate(char* s)
{
String* newString = (String *)malloc(sizeof(String));
newString->Value = malloc(strlen(s) + 1);
newString->Length = strlen(s) + 1;
strcpy(newString->Value, s);
newString->Trim = Trim;
}
However when compiling this in NetBeans (for c, C++) I get the following error:
In file included from String.c:6:
String.h:8: error: expected specifier-qualifier-list before ‘String’
String.c: In function ‘String_Allocate’:
String.c:43: error: ‘String’ has no member named ‘Trim’
make[2]: *** [build/Debug/GNU-Linux-x86/String.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 77ms)
Can anyone help me understand how come the String->Trim member is non-existent and/or how to fix this?
Thank you
struct...