473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problematic recursive include with String and Exception

Hey everyone!

So I'm trying to write a library which mimics the Java API using C++
(http://sf.net/projects/jalp4cpp). I've got some stuff done, but I
can't get the following code to work properly:

class String;

class Exception
{
public:
Exception();
Exception(String s);
String getMessage() const;
private:
String string;
};

class String
{
public:
String();
String(std::string s);
char charAt() const throw(Exception);
public:
std::string string;
};

Needless to say, the above dies with:

Exception.hpp:15: error: field 'string' has incomplete type

But if I try to flip the definition of String and Exception above, I
get the same error with Exception (because of the throw declaration).

I tried playing around with forward declarations, and #ifndef
#include-type stuff, and I can't get this to work.

I guess more importantly, I'm also trying to figure out why this
DOESN'T work. I mean, the code makes sense to me, but I'm not a
compiler. ;)

Thanks, everyone!

Feb 28 '06 #1
3 2758
Oops! The link was actually http://sourceforge.net/projects/jal4cpp,
heh. :)

Mar 1 '06 #2
Karl wrote:
Hey everyone!

So I'm trying to write a library which mimics the Java API using C++
(http://sf.net/projects/jalp4cpp). I've got some stuff done, but I
can't get the following code to work properly:

class String;

class Exception
{
public:
Exception();
Exception(String s);
String getMessage() const;
private:
String string;
};

class String
{
public:
String();
String(std::string s);
char charAt() const throw(Exception);
public:
private?
std::string string;
};

Needless to say, the above dies with:

Exception.hpp:15: error: field 'string' has incomplete type

Swap them and add

class Exception;

before String.

Exception is using a String, so String must be fully defined before
Exception.

String has a throw clause (which may be more trouble than it's worth),
which doesn't require the exception type to be fully defined.

Ian Collins.
Mar 1 '06 #3

Karl wrote:
Hey everyone!

So I'm trying to write a library which mimics the Java API using C++
(http://sf.net/projects/jalp4cpp). I've got some stuff done, but I
can't get the following code to work properly:

class String;

class Exception
{
public:
Exception();
Exception(String s);
String getMessage() const;
private:
String string;
};

class String
{
public:
String();
String(std::string s);
char charAt() const throw(Exception);
public:
std::string string;
};

Needless to say, the above dies with:

Exception.hpp:15: error: field 'string' has incomplete type

But if I try to flip the definition of String and Exception above, I
get the same error with Exception (because of the throw declaration).

I tried playing around with forward declarations, and #ifndef
#include-type stuff, and I can't get this to work.

I guess more importantly, I'm also trying to figure out why this
DOESN'T work. I mean, the code makes sense to me, but I'm not a
compiler. ;)

Thanks, everyone!


There's no way to solve the problem as long as Exception contains a
String member and a String method names Exception in its exception
specification. A type used in an exception specification must be (or be
a pointer or reference to) a complete type.

Personally, I would just turn the exception specification into a
comment. Otherwise the program shuts down were the routine (or one of
its called routines) ever throw something other than an Exception -
which can be difficult to ensure in practice; and at any rate having
the app quit suddenly without explanation is usually not the reason for
declaring the exception specification in the first place. Nor is it all
that surprising why exception specifications, other than throw(), are
rarely used in C++.

Greg

Mar 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
6
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". It's bad luck to name an interface after its implementation. This file could have been...
3
by: Babikie | last post by:
Write a program that performs a reverse recursion with following functions. void swop (char,int,int); void reverse (char); void rev(char,int, int); User should enter a string and all character...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.