Thread overview
"need `this` for `s` of type `char*`" error message
Nov 11, 2021
rempas
Nov 11, 2021
Mathias LANG
Nov 11, 2021
Mathias LANG
Nov 11, 2021
rempas
November 11, 2021

After not being able to use ImportC to automatically compile a C library, now I'm trying to do it myself. The library I want to use is tomlc99 which is a small C library to parse toml files. Now I compiled the library and I copied the "config.h" file and modified to make it a D file that will contain the declaration so D knows which symbols to call. I resulted with a "toml.d" file and a "main.d" file. The first one contains the decorations and the second one contains the code from the example in the usage section. However, when I try to compile, I'm getting the following error message:

main.d(51): Error: need `this` for `s` of type `char*`
Deprecation: argument `__error` for format specification `"%s"` must be `char*`, not `_error_`
main.d(57): Error: need `this` for `i` of type `long`
main.d(63): Error: need `this` for `s` of type `char*`

I uploaded the modified files so someone is able to look at them and explain me what I'm doing wrong so I can properly learn. Links:

toml.d
main.d

November 11, 2021

On Thursday, 11 November 2021 at 07:04:57 UTC, rempas wrote:

>

After not being able to use ImportC to automatically compile a C library, now I'm trying to do it myself. The library I want to use is tomlc99 which is a small C library to parse toml files. Now I compiled the library and I copied the "config.h" file and modified to make it a D file that will contain the declaration so D knows which symbols to call. I resulted with a "toml.d" file and a "main.d" file. The first one contains the decorations and the second one contains the code from the example in the usage section. However, when I try to compile, I'm getting the following error message:

main.d(51): Error: need `this` for `s` of type `char*`
Deprecation: argument `__error` for format specification `"%s"` must be `char*`, not `_error_`
main.d(57): Error: need `this` for `i` of type `long`
main.d(63): Error: need `this` for `s` of type `char*`

I uploaded the modified files so someone is able to look at them and explain me what I'm doing wrong so I can properly learn. Links:

toml.d
main.d

Your type definition is wrong:
```D
struct toml_datum_t {
  int ok;
  union u {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
}
```

If you check the size of this struct, it's going to be 4, because `u` is a type definition. What you want is either:
```D
struct toml_datum_t {
  int ok;
  union {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
}
```

Which you access via `host.s` or:
```D
struct toml_datum_t {
  int ok;
  /// This is the type definition
  union U {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
  /// This is the field
  U u;
}
```

Note that instead of doing this work yourself, I would highly recommend the excellent dstep.

November 11, 2021

On Thursday, 11 November 2021 at 07:10:39 UTC, Mathias LANG wrote:

>

[...]

Your type definition is wrong:

struct toml_datum_t {
  int ok;
  union u {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
}

If you check the size of this struct, it's going to be 4,
because u is a type definition. What you want is either:

struct toml_datum_t {
  int ok;
  union {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
}

Which you access via host.s or:

struct toml_datum_t {
  int ok;
  /// This is the type definition
  union U {
    toml_timestamp_t* ts; /* ts must be freed after use */
    char*   s; /* string value. s must be freed after use */
    int     b; /* bool value */
    int64_t i; /* int value */
    double  d; /* double value */
  }
  /// This is the field
  U u;
}

Note that instead of doing this work yourself, I would highly recommend the excellent dstep.

Fixed formatting (so much for "Fix it for me").

November 11, 2021

On Thursday, 11 November 2021 at 07:15:04 UTC, Mathias LANG wrote:

>

[...]

Note that instead of doing this work yourself, I would highly recommend the excellent dstep.

Fixed formatting (so much for "Fix it for me").

Oh, now it makes sense! Thanks a lot! I will learn and use dstep in the future! Have an amazing day!