digitalmars.D.learn - Newbie needs help with getting (what should be a) simple program to compile...
- Tyler K. (22/22) Dec 06 2005 I'm having a problem in this simple program trying to create an object a...
- Chris Miller (17/36) Dec 06 2005 configfile only exists in your try block. Declare it where enough scopes...
- James Dunne (25/51) Dec 06 2005 First and foremost, welcome to D!
- Frank Benoit (16/16) Dec 07 2005 try
- Tyler K. (1/1) Dec 07 2005 Thanks everyone! I declared it outside of the try block and it works no...
I'm having a problem in this simple program trying to create an object and use
it. This is my first attempt at anything object-oriented (I know that D
probably isn't the best place to start out with no OO experience, but it is a
very appealing language to me coming from a C backgroung). Here is the relevant
code:
...
try
{
__CFileEx configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
configfile.readLine(configlinetemp);
...
__CFileEx is an extention to the CFile class I wrote that handles opening the
file in addition to being a stream wrapper for C files (and a few other
functions specific to my program). The problem I have is that when I try to use
the configfile object, the compiler (DMD 0.140 for Linux) complains that about
undefined identifier configfile and that type int doesn't have property
readLine. What am I doing wrong?
Dec 06 2005
On Tue, 06 Dec 2005 23:32:49 -0500, Tyler K. <Tyler_member pathlink.com>
wrote:
try
{
__CFileEx configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
configfile.readLine(configlinetemp);
...
__CFileEx is an extention to the CFile class I wrote that handles
opening the
file in addition to being a stream wrapper for C files (and a few other
functions specific to my program). The problem I have is that when I
try to use
the configfile object, the compiler (DMD 0.140 for Linux) complains that
about
undefined identifier configfile and that type int doesn't have property
readLine. What am I doing wrong?
configfile only exists in your try block. Declare it where enough scopes
have access:
__CFileEx configfile;
try
{
configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
// Note: if __CFileEx constructor fails (throws), configfile will remain
null.
// You should reconsider the location of the next line.
configfile.readLine(configlinetemp);
Dec 06 2005
Tyler K. wrote:
I'm having a problem in this simple program trying to create an object and use
it. This is my first attempt at anything object-oriented (I know that D
probably isn't the best place to start out with no OO experience, but it is a
very appealing language to me coming from a C backgroung). Here is the
relevant
code:
...
try
{
__CFileEx configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
configfile.readLine(configlinetemp);
...
__CFileEx is an extention to the CFile class I wrote that handles opening the
file in addition to being a stream wrapper for C files (and a few other
functions specific to my program). The problem I have is that when I try to
use
the configfile object, the compiler (DMD 0.140 for Linux) complains that about
undefined identifier configfile and that type int doesn't have property
readLine. What am I doing wrong?
First and foremost, welcome to D!
Second and most importantly - your answer to your question:
It's a problem of scope, nothing new from C. You've defined your
__CFileEx configfile in the try { } block's scope. You can't access
that variable outside the try block.
Simple solution. Change your code to this:
__CFileEx configfile;
try
{
configfile = new __CFileEx("filename", "w+")
}
catch(OpenException oe)
{
/*Handle exception*/
}
configfile.readLine(configlinetemp);
Also, I'd recommend not using double-underscore to prefix your class
names as
1) they are reserved by the D language specification for
implementation purposes. (slightly surprised the compiler doesn't yell
at you for this)
2) they're just plain fugly.
Enjoy delving into the world of D. Lot's of experienced people can
surely help you out here!
Dec 06 2005
try
{
__CFileEx configfile = new __CFileEx("filename", "w+")
char[] configlinetemp;
configfile.readLine(configlinetemp);
}
catch(OpenException oe)
{
/*Handle exception*/
}
As the others said, configfile is local to the scope it is defined in. If
you place the use of configfile behind the catch, you have to check if
configfile is not null. So it is better to make all the work with
configfile inside the try block. If OpenException is thrown, you probably
don't want to read from it.
.
Dec 07 2005
Thanks everyone! I declared it outside of the try block and it works now.
Dec 07 2005









"Chris Miller" <chris dprogramming.com> 