Class Usage

Discussion in 'C++' started by Michael R. Copeland, Nov 9, 2007.

  1. I have defined the following class:
    //-----------------------------------------------------------------
    class LogFunctions // Log/Audit File class
    {
    public:
    LogFunctions::LogFunctions(char *fileName); // constructor
    LogFunctions::~LogFunctions(); // destructor

    void LogFunctions::eek:penLog(char *fileName); // open Log file
    void LogFunctions::putLog(char *line); // write->Log/Audit file
    void LogFunctions::scanLog(); // scan Log/Audit file
    void LogFunctions::closeLog(); // close current log/audit file
    static int listFile(char *fileName); // lists named file

    private:
    static void cursor(char row, char col);
    static void initScr(void);
    static char *fileReadRecord(long);
    static void parseStr(char *str); // parse string from file buffer

    typedef unsigned long ULONG;
    };
    //-----------------------------------------------------------------
    and I am having difficulty using this class as an object pointer. I
    need to use it several times (with different files) throughout the
    execution of my application. That is, I want to have several files
    concurrently opened, writing, being scanned, etc. within the same
    program. Thus, I thought would do the following:
    //-----------------------------------------------------------------
    LogFunctions *l_c = NULL;
    ....
    LogFunctions *l_c = new LogFunctions("testfile.log");
    ....
    l_c->putLog("Some textual data");
    l_c->scanLog();
    etc.
    ....
    l_c->closeLog();
    delete(l_c);
    //-----------------------------------------------------------------
    This sort of code generates all sorts of compile errors (which don't
    help me understand what I did wrong), and I know I've badly confused the
    concepts of class definition and instantiation...8<}}
    So, if my explanation of my intent makes sense, I'd appreciate some
    guidance as to how actually implement this logic... TIA
    Michael R. Copeland, Nov 9, 2007
    #1
    1. Advertising

  2. Michael R. Copeland wrote:
    > I have defined the following class:
    > //-----------------------------------------------------------------
    > class LogFunctions // Log/Audit File class
    > {
    > public:
    > LogFunctions::LogFunctions(char *fileName); // constructor


    Should be

    LogFunctions(char const*);

    > LogFunctions::~LogFunctions(); // destructor


    Should be

    ~LogFunctions();

    If you don't see it yet, there is no need to prepend the names of the
    member function declarations with the class name and '::' _inside_ the
    class definition.

    >
    > void LogFunctions::eek:penLog(char *fileName); // open Log file


    Again, a pointer to _const_ char.

    > void LogFunctions::putLog(char *line); // write->Log/Audit file


    Again, a pointer to _const_ char.

    > void LogFunctions::scanLog(); // scan Log/Audit file


    What does it mean to "scan" it?

    > void LogFunctions::closeLog(); // close current log/audit file
    > static int listFile(char *fileName); // lists named file


    Again, a pointer to _const_ char.

    >
    > private:
    > static void cursor(char row, char col);
    > static void initScr(void);


    If there are no arguments, it's better to have empty parentheses, as
    in

    static void initScr();

    > static char *fileReadRecord(long);
    > static void parseStr(char *str); // parse string from file buffer


    Again, a pointer to _const_ char.

    >
    > typedef unsigned long ULONG;


    I suppose there is some use for this...

    > };
    > //-----------------------------------------------------------------
    > and I am having difficulty using this class as an object pointer. I
    > need to use it several times (with different files) throughout the
    > execution of my application. That is, I want to have several files
    > concurrently opened, writing, being scanned, etc. within the same
    > program. Thus, I thought would do the following:
    > //-----------------------------------------------------------------
    > LogFunctions *l_c = NULL;


    Why do youi need to declare it here? And *where* is that?

    > ...
    > LogFunctions *l_c = new LogFunctions("testfile.log");


    You seem to be declaring *another* object with the name 'l_c' here.
    Do you really need two of them?

    > ...
    > l_c->putLog("Some textual data");
    > l_c->scanLog();
    > etc.
    > ...
    > l_c->closeLog();
    > delete(l_c);


    Parentheses? Why?

    > //-----------------------------------------------------------------
    > This sort of code generates all sorts of compile errors (which don't
    > help me understand what I did wrong), and I know I've badly confused
    > the concepts of class definition and instantiation...8<}}


    *What* sorts of compile errors? I cannot get them since the code you
    posted is not real -- it's an exerpt.

    > So, if my explanation of my intent makes sense, I'd appreciate some
    > guidance as to how actually implement this logic... TIA


    Have you tried looking on the web? If you haven't, do, and look for
    'log4cpp' project. There is no need to reinvent the wheel. Just find
    what you need and use it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 9, 2007
    #2
    1. Advertising

  3. > > I have defined the following class:
    > > //-----------------------------------------------------------------
    > > class LogFunctions // Log/Audit File class
    > > {
    > > public:
    > > LogFunctions::LogFunctions(char *fileName); // constructor

    >
    > Should be
    >
    > LogFunctions(char const*);
    >
    > > LogFunctions::~LogFunctions(); // destructor

    >
    > Should be
    >
    > ~LogFunctions();
    >
    > If you don't see it yet, there is no need to prepend the names of the
    > member function declarations with the class name and '::' _inside_ the
    > class definition.

    Okay...

    > > void LogFunctions::scanLog(); // scan Log/Audit file

    >
    > What does it mean to "scan" it?

    This function closes the object's file, reopens it to read it,
    displays 25 lines of it at a time, allows the user to "page" back and
    forth through the file's data. It's simply a "listing" function that
    allows the application's user to see the log/audit data that's
    accumulated during execution.

    > > private:
    > > static void cursor(char row, char col);
    > > static void initScr(void);

    >
    > If there are no arguments, it's better to have empty parentheses, as
    > in
    >
    > static void initScr();

    Yes...
    Michael R. Copeland, Nov 9, 2007
    #3
  4. > and I am having difficulty using this class as an object pointer. I
    > need to use it several times (with different files) throughout the
    > execution of my application. That is, I want to have several files
    > concurrently opened, writing, being scanned, etc. within the same
    > program. Thus, I thought would do the following:
    > //-----------------------------------------------------------------
    > LogFunctions *l_c = NULL;


    Why do you need to declare it here? And *where* is that?

    It's in the main line of the program: when I'm opening data files,
    etc.

    > ...
    > LogFunctions *l_c = new LogFunctions("testfile.log");


    You seem to be declaring *another* object with the name 'l_c' here.
    Do you really need two of them?

    Ahhh, no. This is part of my confusion about declaring classes and
    instantiating objects of them for use.

    > ...
    > l_c->putLog("Some textual data");
    > l_c->scanLog();
    > etc.
    > ...
    > l_c->closeLog();
    > delete(l_c);


    Parentheses? Why?

    Just my lack of knowledge/understanding of class usage... 8<{{

    > //-----------------------------------------------------------------
    > This sort of code generates all sorts of compile errors (which don't
    > help me understand what I did wrong), and I know I've badly confused
    > the concepts of class definition and instantiation...8<}}


    *What* sorts of compile errors? I cannot get them since the code you
    posted is not real -- it's an exerpt.

    Well, it wraps my VS6.0 compiler to its limit (102 errors), and there
    are so many I didn't see how I could enumerate them. They start with
    the
    > LogFunctions *l_c = new LogFunctions("testfile.log");

    line.

    > So, if my explanation of my intent makes sense, I'd appreciate some
    > guidance as to how actually implement this logic... TIA


    Have you tried looking on the web? If you haven't, do, and look for
    'log4cpp' project. There is no need to reinvent the wheel. Just find
    what you need and use it.

    I'll do that now. I was trying to establish some "class" knowledge,
    as I thought this was a rather simple application for my usage. Oh
    well...
    Michael R. Copeland, Nov 9, 2007
    #4
  5. Michael R. Copeland wrote:
    >> //-----------------------------------------------------------------
    >> This sort of code generates all sorts of compile errors (which
    >> don't help me understand what I did wrong), and I know I've badly
    >> confused the concepts of class definition and instantiation...8<}}

    >
    > *What* sorts of compile errors? I cannot get them since the code you
    > posted is not real -- it's an exerpt.
    >
    > Well, it wraps my VS6.0 compiler to its limit (102 errors), and
    > there are so many I didn't see how I could enumerate them. They
    > start with the
    >> LogFunctions *l_c = new LogFunctions("testfile.log");

    > line.


    You might be better off if you get yourself a more up-to-date compiler.
    VS6 is very old and just bad. If you can't buy 2005, get the Express
    Edition, it's free.

    >> So, if my explanation of my intent makes sense, I'd appreciate some
    >> guidance as to how actually implement this logic... TIA

    >
    > Have you tried looking on the web? If you haven't, do, and look for
    > 'log4cpp' project. There is no need to reinvent the wheel. Just find
    > what you need and use it.
    >
    > I'll do that now. I was trying to establish some "class" knowledge,
    > as I thought this was a rather simple application for my usage. Oh
    > well...


    Your time is better spent on developing something new, something that
    is original, something that is needed for your future work. Don't waste
    it rewriting something freely available for reuse.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 9, 2007
    #5
  6. Michael R. Copeland

    Jim Langston Guest

    "Michael R. Copeland" <> wrote in message
    news:...
    > I have defined the following class:
    > //-----------------------------------------------------------------
    > class LogFunctions // Log/Audit File class
    > {
    > public:
    > LogFunctions::LogFunctions(char *fileName); // constructor
    > LogFunctions::~LogFunctions(); // destructor
    >
    > void LogFunctions::eek:penLog(char *fileName); // open Log file
    > void LogFunctions::putLog(char *line); // write->Log/Audit file
    > void LogFunctions::scanLog(); // scan Log/Audit file
    > void LogFunctions::closeLog(); // close current log/audit file
    > static int listFile(char *fileName); // lists named file
    >
    > private:
    > static void cursor(char row, char col);
    > static void initScr(void);
    > static char *fileReadRecord(long);
    > static void parseStr(char *str); // parse string from file buffer
    >
    > typedef unsigned long ULONG;
    > };
    > //-----------------------------------------------------------------
    > and I am having difficulty using this class as an object pointer. I
    > need to use it several times (with different files) throughout the
    > execution of my application. That is, I want to have several files
    > concurrently opened, writing, being scanned, etc. within the same
    > program. Thus, I thought would do the following:
    > //-----------------------------------------------------------------
    > LogFunctions *l_c = NULL;


    LogFunctions *l_c = new LogFunctions("testfile.log");

    should allow it to compile as long as you have the rest of the class done
    (actual code).

    But please, read Victors post, there is a lot that should be cleaned up
    (like constant correctness).

    You could also do:
    LogFunctions *l_c = NULL;
    l_c = new LogFunctions("testfile.log");

    But there is no reason to assign it to NULL when on the next line you're
    going to give it a value anyway. Best to just put on one line.

    > ...
    > LogFunctions *l_c = new LogFunctions("testfile.log");
    > ...
    > l_c->putLog("Some textual data");
    > l_c->scanLog();
    > etc.
    > ...
    > l_c->closeLog();
    > delete(l_c);
    > //-----------------------------------------------------------------
    > This sort of code generates all sorts of compile errors (which don't
    > help me understand what I did wrong), and I know I've badly confused the
    > concepts of class definition and instantiation...8<}}
    > So, if my explanation of my intent makes sense, I'd appreciate some
    > guidance as to how actually implement this logic... TIA
    Jim Langston, Nov 10, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metfan
    Replies:
    2
    Views:
    4,832
    Robert Olofsson
    Oct 21, 2003
  2. E11
    Replies:
    1
    Views:
    4,703
    Thomas Weidenfeller
    Oct 12, 2005
  3. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    520
    Colin J. Williams
    Feb 26, 2004
  4. hvt
    Replies:
    0
    Views:
    1,190
  5. hvt
    Replies:
    0
    Views:
    1,447
Loading...

Share This Page