Compilation error in the `glbinding` example, which is supposed to output the OpenGL version

101 views Asked by At

I wanted to understand how the OpenGL loader library glbinding works, but I couldn't compile my very basic example, which is a reduced version of their cubescape example:

#include <iostream>

#include <GLFW/glfw3.h>

#include <glbinding/glbinding.h>
#include <glbinding-aux/ContextInfo.h>

using std::cout;
using std::endl;

int main()
{
  if (glfwInit())
  {
    // ------ create window
    auto const windowPtr = glfwCreateWindow(640, 480, "tc0001", nullptr, nullptr);
    if (windowPtr)
    {
      glfwHideWindow(windowPtr);
      glfwMakeContextCurrent(windowPtr);
      glbinding::initialize(glfwGetProcAddress, false);
      cout << "OpenGL Version:  " << glbinding::aux::ContextInfo::version() << endl;
      cout << "OpenGL Vendor:   " << glbinding::aux::ContextInfo::vendor() << endl;
      cout << "OpenGL Renderer: " << glbinding::aux::ContextInfo::renderer() << endl;
      // ------ destroy window
      glfwDestroyWindow(windowPtr);
    }
    else
    {
      cout << "glfwCreateWindow error" << endl;
    }
    glfwTerminate();
  }
  else
  {
    cout << "glfwInit error" << endl;
  }
}

The culprit is the line:

cout << "OpenGL Version:  " << glbinding::aux::ContextInfo::version() << endl;

The compiler complains about this line, and after that, as usual, outputs a lot of information about failed attempts to find a right implementation of the ‘operator<<’:

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘glbinding::Version’)

What am I doing wrong?

  • OS: Ubuntu 22.04.3 LTS
  • Compiler: g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
  • glbinding: 3.3.0
2

There are 2 answers

0
Rabbid76 On

The version() method returns an object of type glbinding::Version which cannot be automatically converted to a string. You need to use toString():

cout << "OpenGL Version: " << glbinding::aux::ContextInfo::version() << endl;

cout << "OpenGL Version:  " << glbinding::aux::ContextInfo::version().toString() << endl;
2
fjs On

I'm not exactly familiar with the OpenGL library, but from a C++ programming perspective the problem is caused by the fact you are trying to use cout to print a non-trivial type - in the case of cout<<glbinding::aux::ContextInfo::version(), it is class glbinding::Version.

You can use cout to print plain old data types like int, double, also std::string, but for more complex (i.e., arbitrary) classes you'll have to define what data fields of that class cout gets to print, and in what format.

Does the OpenGL library provide an overload definition of operator<< for class glbinding::Version?

If not, but you know the definition of class glbinding::Version, you just need to define an overload of operator << for this class. https://www.learncpp.com/cpp-tutorial/overloading-the-io-operators/