Вопрос

I want to forward declare a struct in the header file.

struct GLFWvidmode;

class DesktopVideoMode {
private:
    const GLFWvidmode *videomode;
public:
    DesktopVideoMode(const GLFWvidmode *videomode);
...

In the cpp file I include the external header with the definition...

#include "DesktopVideoMode.hpp"
#include <GLFW/glfw3.h>

...where the error "Typedef redefinition with different types ('struct GLFWvidmode' vs 'GLFWvidmode')" happens:

typedef struct
{
    /*! The width, in screen coordinates, of the video mode.
     */
    int width;
    /*! The height, in screen coordinates, of the video mode.
     */
    int height;
    /*! The bit depth of the red channel of the video mode.
     */
    int redBits;
    /*! The bit depth of the green channel of the video mode.
     */
    int greenBits;
    /*! The bit depth of the blue channel of the video mode.
     */
    int blueBits;
    /*! The refresh rate, in Hz, of the video mode.
     */
    int refreshRate;
} GLFWvidmode;

Can't I forward declare in a case like this?

Это было полезно?

Решение

GLFWvidmode is not a struct, it's a typedef. You can't forward-declare a typedef. Whoever chose to use an unnamed struct made a poor design decision.

Другие советы

I would like to mention that GLFWvidmode is a typedef name to anonymous struct.. if you purposefully want to forward declare the struct then you should always add a nametag to the struct while declaring the structure as:

    typedef struct tagname1{
    some members...;
    }tagname2;

note dat tagname1 and tagname2 can be same (you can use tagname1 or tagname or GLFWvidmode in both the places).. and now since the struct is now having a tagname(it's not anonymous anymore) you can reference it for forward declaration.

and yes an anonymous struct can't be used for forward declaration as there is no tagname to reference to.. :) hope it helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top