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
Answers
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 classglbinding::Version
.You can use cout to print plain old data types like
int
,double
, alsostd::string
, but for more complex (i.e., arbitrary) classes you’ll have to define what data fields of that classcout
gets to print, and in what format.Does the OpenGL library provide an overload definition of
operator<<
for classglbinding::Version
?If not, but you know the definition of class
glbinding::Version
, you just need to define an overload ofoperator <<
for this class. https://www.learncpp.com/cpp-tutorial/overloading-the-io-operators/#include <glbinding-aux/types_to_string.h>
like thecubescape
demo does so your program can access theoperator<<()
overload for theglbinding::Version
type.