I have written a program in which i use C++ stl set. There is a struct event from which the set is being constructed, and its corresponding binary predicate.. struct comp to define the ordering between them in the set.
The code portion looks as follows:
struct event
{
int s;
int f;
int w;
set<event,comp>::iterator nxt;
};
struct comp
{
bool operator()(event a, event b)
{
if(a.f!=b.f)
return a.f<b.f;
else
{
if(a.s!=b.s)
return a.s<b.s;
else
return a.w>b.w;
}
}
};
set< event , comp > S;
The problem I am facing here is which struct to write first? I have tried forward-declaring both the structs. I have compiler errors in both the cases.
You need to include both the definitions before you create the
std::setobject:Forward declarations won’t work for you because once you forward declare a type it becomes an incomplete type and in this case the compiler needs to know the layout and size of both the types. Incomplete types work only when the compiler does not need to know the size or the layout of the type for e.x: pointer to the type, since all pointers have same size.