C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

recursive definitions for header files

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Karl
Guest





PostPosted: Thu Feb 23, 2006 1:06 am    Post subject: recursive definitions for header files Reply with quote



Hey everyone!

I'm working on a C++ library which mimics the Java API
(http://sourceforge.net/projects/jal4cpp). The problem I've come
across is one of recursive definitions which I cannot get preprocessor
hacks (i.e., #ifndef...#define...#endif) to fix.

So I have a class called jal::lang::String which is meant to mimic the
java.lang.String class.

#ifndef STRING_HPP
#define STRING_HPP

#include <string>
#include <ostream>

//#include "jal/lang/IndexOutOfBoundsException.hpp"
#include "jal/lang/Comparable.hpp"
#include "jal/lang/Object.hpp"
#include "jal/lang/CharSequence.hpp"

namespace jal
{
namespace lang
{

class String : public Comparable<String>, public Object<String>,
public CharSequence
{
public:
String();
String(std::string s);
String(const char* s);
virtual int compareTo(const String& s) const;
virtual const char* toCString() const;
virtual const std::string& toStdString() const;
virtual bool equals(const String& s) const;
virtual int length() const;
//virtual char charAt() const
throw(jal::lang::IndexOutOfBoundsException);

virtual ~String();
private:
std::string mString;
};

}
}

std::ostream& operator <<(std::ostream& os, const jal::lang::String&
s);


#endif

As you can see, it subclasses jal::lang::CharSequence for its length()
method (among others).

This works great.

If you look again, you can see that I have a #include commented out on
line 7 on String.hpp:

//#include "jal/lang/IndexOutOfBoundsException.hpp"

IndexOutOfBoundsException.hpp is as follows:

#ifndef INDEXOUTOFBOUNDSEXCEPTION_H
#define INDEXOUTOFBOUNDSEXCEPTION_H

#include "jal/lang/RuntimeException.hpp"

namespace jal
{
namespace lang
{
class IndexOutOfBoundsException : public RuntimeException
{
public:
IndexOutOfBoundsException(jal::lang::String message);
IndexOutOfBoundsException();
virtual ~IndexOutOfBoundsException() throw();

};

}
}

#endif

If I uncomment the line #include'ing IndexOutOfBoundsException.hpp in
String.hpp (as shown above), I get the following error message in g++
whenever I try to use a jal::lang::String.

../jal/lang/Exception.hpp:15: error: expected `)' before 's'
../jal/lang/Exception.hpp:17: error: 'String' in namespace 'jal::lang'
does not name a type
../jal/lang/Exception.hpp:21: error: 'String' in namespace 'jal::lang'
does not name a type
../jal/lang/RuntimeException.hpp:13: error: expected `)' before
'message'
../jal/lang/IndexOutOfBoundsException.hpp:13: error: expected `)' before
'message'

Now, this stumped me for a while, but then I got an epiphany: The
String.hpp includes IndexOutOfBoundsException.hpp, which--in
turn--tries to include String.hpp. So they depend on each other, which
is bad.

Is my thinking flawed? Can anyone enlighten me? Can I get around
this?

I thank anyone ahead of time who's willing to help me out.

P.S. You can view all the code directly at
http://cvs.sourceforge.net/viewcvs.py/jal4cpp/CVSROOT/JAL4C++/jal/lang/


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Thu Feb 23, 2006 3:06 pm    Post subject: Re: recursive definitions for header files Reply with quote



Hi,

Quote:
I'm working on a C++ library which mimics the Java API
(http://sourceforge.net/projects/jal4cpp). The problem I've come
across is one of recursive definitions which I cannot get preprocessor
hacks (i.e., #ifndef...#define...#endif) to fix.

//#include "jal/lang/IndexOutOfBoundsException.hpp"
#include "jal/lang/Comparable.hpp"
#include "jal/lang/Object.hpp"
#include "jal/lang/CharSequence.hpp"

/* snip */

Quote:
As you can see, it subclasses jal::lang::CharSequence for its length()
method (among others).

This works great.

If you look again, you can see that I have a #include commented out on
line 7 on String.hpp:

//#include "jal/lang/IndexOutOfBoundsException.hpp"

IndexOutOfBoundsException.hpp is as follows:

/* snip */
Quote:
namespace jal
{
namespace lang
{
class IndexOutOfBoundsException : public RuntimeException
{
public:
IndexOutOfBoundsException(jal::lang::String message);

/* snip */

Quote:
If I uncomment the line #include'ing IndexOutOfBoundsException.hpp in
String.hpp (as shown above), I get the following error message in g++
whenever I try to use a jal::lang::String.

./jal/lang/Exception.hpp:15: error: expected `)' before 's'
./jal/lang/Exception.hpp:17: error: 'String' in namespace 'jal::lang'
does not name a type
./jal/lang/Exception.hpp:21: error: 'String' in namespace 'jal::lang'
does not name a type
./jal/lang/RuntimeException.hpp:13: error: expected `)' before
'message'
./jal/lang/IndexOutOfBoundsException.hpp:13: error: expected `)' before
'message'

Now, this stumped me for a while, but then I got an epiphany: The
String.hpp includes IndexOutOfBoundsException.hpp, which--in
turn--tries to include String.hpp. So they depend on each other, which
is bad.

Is my thinking flawed? Can anyone enlighten me? Can I get around
this?

The problem is here that you are passing the strings around as
objects, thus the compiler requires the complete type. Why that?
Wouldn't a reference to a constant string good enough here?

Besides, if you mimic the Java API, shouldn't all the object arguments
be references in first place?

So long,
Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Thu Feb 23, 2006 3:06 pm    Post subject: Re: recursive definitions for header files Reply with quote



Karl wrote:

Quote:
I'm working on a C++ library which mimics the Java API
(http://sourceforge.net/projects/jal4cpp). The problem I've
come across is one of recursive definitions which I cannot get
preprocessor hacks (i.e., #ifndef...#define...#endif) to fix.

So I have a class called jal::lang::String which is meant to
mimic the java.lang.String class.

#ifndef STRING_HPP
#define STRING_HPP

#include <string
#include <ostream

//#include "jal/lang/IndexOutOfBoundsException.hpp"
#include "jal/lang/Comparable.hpp"
#include "jal/lang/Object.hpp"
#include "jal/lang/CharSequence.hpp"

namespace jal
{
namespace lang
{

class String : public Comparable<String>, public Object<String>,
public CharSequence
{
public:
String();
String(std::string s);
String(const char* s);
virtual int compareTo(const String& s) const;
virtual const char* toCString() const;
virtual const std::string& toStdString() const;
virtual bool equals(const String& s) const;
virtual int length() const;
//virtual char charAt() const
throw(jal::lang::IndexOutOfBoundsException);

Why the throw clause here? What does it buy you?

If I'm not mistaken, IndexOutOfBoundsException is a
RuntimeException in Java, which means that it doesn't need to be
declared in the exception specification. But more to the point,
exception specifications do not mean the same thing in Java and
in C++, and so it is normal that their use in one language does
not map into their use in the other. (Normally, exceptions that
require an exception specification in Java are better handled by
means of a return code in C++.)

Not that this is relevant to your more general problem, although
it would solve it here, because there would be no need to
include the header with the exception.

Quote:
virtual ~String();
private:
std::string mString;
};
}
}
#endif

If you look again, you can see that I have a #include
commented out on line 7 on String.hpp:

//#include "jal/lang/IndexOutOfBoundsException.hpp"

IndexOutOfBoundsException.hpp is as follows:

#ifndef INDEXOUTOFBOUNDSEXCEPTION_H
#define INDEXOUTOFBOUNDSEXCEPTION_H

#include "jal/lang/RuntimeException.hpp"

namespace jal
{
namespace lang
{
class IndexOutOfBoundsException : public RuntimeException
{
public:
IndexOutOfBoundsException(jal::lang::String message);
IndexOutOfBoundsException();
virtual ~IndexOutOfBoundsException() throw();

};
}
}
#endif

If I uncomment the line #include'ing
IndexOutOfBoundsException.hpp in String.hpp (as shown above),
I get the following error message in g++ whenever I try to use
a jal::lang::String.

./jal/lang/Exception.hpp:15: error: expected `)' before 's'
./jal/lang/Exception.hpp:17: error: 'String' in namespace 'jal::lang'
does not name a type
./jal/lang/Exception.hpp:21: error: 'String' in namespace 'jal::lang'
does not name a type
./jal/lang/RuntimeException.hpp:13: error: expected `)' before
'message'
./jal/lang/IndexOutOfBoundsException.hpp:13: error: expected `)' before
'message'

Now, this stumped me for a while, but then I got an epiphany:
The String.hpp includes IndexOutOfBoundsException.hpp,
which--in turn--tries to include String.hpp. So they depend
on each other, which is bad.

Is my thinking flawed? Can anyone enlighten me? Can I get
around this?

Sure. The definition of String, in String.hh, doesn't depend on
the actual definition of IndexOutOfBoundsException in any way.
If you insist on the exception specification, it does require
that the compiler know that there is a class named
IndexOutOfBoundsException, but no more. A simple
class IndexOutOfBoundsException ;
(in the correct namespace, of course) is sufficient.

If you have two classes which mutually depend on the definition
of each other, you do have a problem. But I can't off hand see
how this could occur.

In Java, of course, the compiler does need some means of
handling such mutual dependencies, because there is no clean
separation of interface and implementation. But C++ is not
Java, and while the C++ model in this regard is far from
perfect, it is sufficent to avoid this problem.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.