Typedef & confused ADT

  • Thread starter Thread starter fyderniX
  • Start date Start date
F

fyderniX

Heya. I have a header file that looks similar to this:

namespace mupdf {

public class Data
{
public:
typedef struct fz_stream_s fz_stream;

struct fz_stream_s
{
int refs;
int kind;
int mode;
int dead;
fz_buffer *buffer;
fz_filter *filter;
fz_stream *chain;
fz_error *error;
int file;
};
};
}

When I try to create"mupdf::Data::fz_stream fileStream;" it gives me
grief, however:

..\sandbox2.cpp(18) : error C2079: 'fileStream' uses undefined struct
'mupdf::fz_stream_s'

"fz_stream_s" clearly exists from within the "Data" class. Why is the
compiler looking for it way up in the "mupdf" namespace? What am I
doing wrong?

SigmaX
 
Heya. I have a header file that looks similar to this:

namespace mupdf {

public class Data
{
public:
typedef struct fz_stream_s fz_stream;

struct fz_stream_s
{
int refs;
int kind;
int mode;
int dead;
fz_buffer *buffer;
fz_filter *filter;
fz_stream *chain;
fz_error *error;
int file;
};
};
}

When I try to create"mupdf::Data::fz_stream fileStream;" it gives me
grief, however:

.\sandbox2.cpp(18) : error C2079: 'fileStream' uses undefined struct
'mupdf::fz_stream_s'

"fz_stream_s" clearly exists from within the "Data" class. Why is the
compiler looking for it way up in the "mupdf" namespace? What am I
doing wrong?

Types that are first encountered in declarations such as this interpreted
as living in the enclosing namespace. To solve the problem, forward declare
the type:

namespace mupdf {

public class Data
{
public:
struct fz_stream_s;
typedef struct fz_stream_s fz_stream;

struct fz_stream_s
{
int refs;
int kind;
int mode;
int dead;
fz_buffer *buffer;
fz_filter *filter;
fz_stream *chain;
fz_error *error;
int file;
};
};
}

This tells the compiler that fz_stream_s is a member of Data and not the
enclosing namespace. That said, I don't know why you don't go with the
simpler:

namespace mupdf {

public class Data
{
public:

struct fz_stream
{
int refs;
int kind;
int mode;
int dead;
fz_buffer *buffer;
fz_filter *filter;
fz_stream *chain;
fz_error *error;
int file;
};
};
}
 
Types that are first encountered in declarations such as this interpreted
as living in the enclosing namespace. To solve the problem, forward declare
the type:

Thanx. That should be what I needed to know.
That said, I don't know why you don't go with the
simpler:

namespace mupdf {

public class Data
{
public:

struct fz_stream
{
int refs;
int kind;
int mode;
int dead;
fz_buffer *buffer;
fz_filter *filter;
fz_stream *chain;
fz_error *error;
int file;
};
};

}

Well, I'm actually writing a wrapper for a library somebody else made,
and that's how they set up most all their structs. I'm not sure their
reasoning for the alias, but I figured I'd use it just in case it's
important ;-).

SigmaX
 
Well, I'm actually writing a wrapper for a library somebody else made,
and that's how they set up most all their structs. I'm not sure their
reasoning for the alias, but I figured I'd use it just in case it's
important ;-).

Is that library perhaps written in C, not C++? In that case, you'd have to
specify "struct Blah" everywhere, the typedef allows you to avoid needing
the keyword "struct" everywhere. But C++ doesn't need the extra "struct",
even without a typedef.
 
Back
Top