Search code examples
c++gtk

GDK: Expression must be a modifiable value


I have a function which works with GdkPixbufs (simplified):

void test_function(std::vector<GdkPixbuf> &images)
{
    std::vector<std::filesystem::path> filenames = {"path/a", "path/b"};
    for(int i = 0; i < 2; i++)
    {
        images[i] = *gdk_pixbuf_new_from_file(filenames[i].string().c_str(), NULL);
    }
    return;
}

GCC throws expression must be a modifiable value error at the images assignment line. Everything seems fine to me. Why does it happen?


Solution

  • Compiling on Gtk4, I got this error,

    usr/include/c++/12/bits/stl_vector.h:1124:41: error: invalid use of incomplete type ‘struct _GdkPixbuf’
     1124 |         return *(this->_M_impl._M_start + __n);
          |                 ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
    /usr/include/gdk-pixbuf-2.0/gdk-pixbuf/gdk-pixbuf-core.h:89:16: note: forward declaration of ‘struct _GdkPixbuf’
       89 | typedef struct _GdkPixbuf GdkPixbuf;
    

    Incomplete type means you should always refer to the type with a pointer. You can only have a vector of std::vector<GdkPixbuf*>, but not std::vector<GdkPixbuf>.

    This is also the expected way to work with other gtk widgets.

    If you are unfamiliar with incomplete type(or opaque type), the following question servers a good reference.

    What is an opaque value in C++?