I have got this hpp file:
struct rte_spinlock_t;
class A {
public:
void init();
private:
rte_spinlock_t* spinlock;
};
and the corresponding cpp file:
#include "A.hpp"
typedef struct {
int lock;
} rte_spinlock_t;
void A::init()
{
}
Now, compiling like this: g++ A.cpp I get this error:
A.cpp:5:3: error: conflicting declaration ‘typedef struct rte_spinlock_t rte_spinlock_t’
5 | } rte_spinlock_t;
| ^~~~~~~~~~~~~~
In file included from A.cpp:1:
A.hpp:2:8: note: previous declaration as ‘struct rte_spinlock_t’
2 | struct rte_spinlock_t;
| ^~~~~~~~~~~~~~
Obviously making the struct named it works, unfortunately I cannot control the typedef of struct rte_spinlock_t that is in a library.
How could I workaround this?
I expect to be able to forward an unnamed struct without getting into conflicting declaration.
From what I understand what you have is more-less this (erroneous code): https://godbolt.org/z/zarsTq6oE
Why don't you use pimpl (private implementation) idiom for extra level of indirection and hiding the "gory details"?
Let's assume my
X
is yourA
,lib.h
contains the troublesome typedef:Live demo: https://godbolt.org/z/4jYGrYEjr
BTW, I made an assumption that your troublesome struct is C code based on some searching...