Struct foo<T> missing init

Hello, good people of the intrawebs!
Today I bring you a bit of a conundrum, namely a structthat makes the linker think it needs an init function.

  /**
   * Represents a pair of of values of type T.
   */
  public struct Pair<T> {
    T v0;
    T v1;
    
    
    /**
     * Create a Pair with two values.
     */
    Pair (T v0, T v1) {
      this.v0 = v0; this.v1 = v1;
    }

    /**
     * Create a Pair without any values set
     */
    Pair.empty () {}

    /**
     * Get a value.
     *
     * @note you should probably not use this as it is slower than using the
     * internal values directly.
     */
    T @get (size_t index) {
      switch (index) {
        case 0:
          return v0;
        case 1:
          return v1;
        default:
          return (T)null;
      }
    }

    /**
     * set a value
     *
     * @note you should probably not use this as it is slower than using the
     * internal values directly.
     */
    void @set (size_t index, T value) {
      switch (index) {
        case 0:
          this.v0 = value;
          break;
        case 1:
          this.v1 = value;
          break;
        default:
          return;
      }
    }
  }

The compiler compiled that portion fine, but when I try to use it as such:

    Pair p = Pair<int> (_height, _width);

I get the following error in the linking stage:

undefined reference to `pair_init'

As I understand it, a struct should not have an init function, so, what is wrong here?

Other information:
$ valac --version
Vala 0.42.2

Help would be appreciated.

1 Like

pair_init is just the C name of the constructor. You can use valac -C to look at the generated C code.

It works for me with Vala 0.44.3 with this main function:

void main () {
    var p = Pair<int> (0, 1);
}

You can also use

Pair<int> p = {0, 1};

which will not use the constructor

1 Like

I’ll have a look when I come home, but if it is any help, I’m returning the Pair as an owned object in a property getter. Can it be a problem with that?

Perhaps the code gen does something it should not be doing because of the context?

I’ll look later.

Sooo…
I’ve been looking at the generated code that ninja creates, and can’t find what is wrong.

The C file has nothing wrong with it, it generates the correct functions:

/**
     * Create a Pair with two values.
     */
static void
vprocessing_util_structure_pair_init (vProcessingUtilStructurePair *self,
                                      gconstpointer v0,
                                      gconstpointer v1)
{
#line 13 "../src/utils/structure.vala"
	memset (self, 0, sizeof (vProcessingUtilStructurePair));
#line 14 "../src/utils/structure.vala"
	(*self).v0 = v0;
#line 14 "../src/utils/structure.vala"
	(*self).v1 = v1;
#line 83 "structure.c"
}

I thought it might have been something to do with the order of which I declare the files in the Meson file, but that did not help.

The symbol exists in the generated object file too…

It seems that the generated ninja script does not know what to do. It does not do the linking stage correctly.

Would anyone be as kind as to take a look at my project, I don’t know if I’m doing everything correctly. (The Meson things, that is)

Here is the code:
https://code.launchpad.net/~gustav-hartvigsson/vprocessing/rewrite

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.