Definition order of POD

H

hurcan solter

I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;


struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....
 
M

mlimber

I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;

struct a
{
b bval;

};

struct b
{
int aval;};

which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....

You might employ some sort of topological sort (cf. <http://
en.wikipedia.org/wiki/Topological_sort> and <http://www.ddj.com/blog/
cppblog/archives/2006/10/abstraction_and.html>).

Cheers! --M
 
V

Victor Bazarov

hurcan said:
I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;


struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.

Yes, that code is invalid.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.

Even if you split them, you still need 'b' to be defined when 'a' is
being processed.
is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?

Circumvent what exactly? The code as it stands, is invalid. There
is nothing that can be done to it, short of rewriting it to make it
valid.

How are you "generating" those PODs? Is that your C++ program or is
that something else, like a Perl script or a Python program? We can
only help with C++ code. When you generate those, you could try to
write the definition of 'a' and check if 'b' has been defined, and
if it hasn't, hold putting the definition of 'a' out until 'b' is
defined. But then you would be writing part of the compiler.

Fixing the program that produces your XML may actually be a more
viable solution.

V
 
H

hurcan solter

Even if you split them, you still need 'b' to be defined when 'a' is
being processed.
I thought it was obvious that i will generate necessary "#include"s
if i go that way.with some include guards it should be fairly
simple
and straightforward.
Circumvent what exactly? The code as it stands, is invalid. There
is nothing that can be done to it, short of rewriting it to make it
valid.

How are you "generating" those PODs? Is that your C++ program or is
that something else, like a Perl script or a Python program? We can
only help with C++ code. When you generate those, you could try to
write the definition of 'a' and check if 'b' has been defined, and
if it hasn't, hold putting the definition of 'a' out until 'b' is
defined. But then you would be writing part of the compiler.

Fixing the program that produces your XML may actually be a more
viable solution.
I was hoping for some kind of compiler magic I suppose.I'll give
the topological sorting idea a go, thanks for the input though .
cheers
 
S

Sarath

struct a
{
b bval;

};

struct b
{
int aval;};

which wont compile because b is not yet defined when the compiler hits
it.

struct b;
struct a
{
b bval;

};

struct b
{
int aval;};

seems this compile.
 
I

Ivan Vecerina

: >
: > struct a
: > {
: > b bval;
: >
: > };
: >
: > struct b
: > {
: > int aval;};
: >
: > which wont compile because b is not yet defined when the compiler
hits
: > it.
:
: struct b;
: struct a
: {
: b bval;
:
: };
:
: struct b
: {
: int aval;};
:
: seems this compile.

No, it is not valid C++.
It would only compile with a forward-declaration
if a's member was a pointer: b* bval;

A topological sort based on the dependency graph
is the obvious solution...
(hoping that there are no cyclic dependencies,
in which case the use of pointes would be required).


Regards,
Ivan
 
J

Jim Langston

hurcan solter said:
I am generating a bunch of class type PODs from an xml file.The thing
is,xml file has no notion of order of definitions, so I end up in
situations like;


struct a
{
b bval;

};
struct b
{
int aval;
};
which wont compile because b is not yet defined when the compiler hits
it.
I also dont want to split them into different files because there are
numerous
and also like to provide to clients a single point of entry.is there a
way to
avoid or circumvent this situation lest I should have to modify the
xml file itself?
Any help would be greatly appreciated....

If you can use a poiner to b, then you could predefine the clase
class b;
struct a
{
b* bval;
}

However, if you need an instance of b (as your code is showing) then b has
to be defined before a. The easiest way, just change the order your are
declaring your classes. Cut and paste and make it
struct b
{
int aval;
};
struct a
{
b bval;
};
and it will compile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top