Inline class members used in vc 2005

Discussion in 'C++' started by Allen, Dec 5, 2006.

  1. Allen

    Allen Guest

    Last time, i post a topic about inline class member declaration in C++.
    And in VC6, it does not support inline class member.

    Now I upgrade to VC 2005. But the compiler doesnot completely support
    it.

    // Logger.h
    class CLogger
    {
    public:
    static const int nameLen = 16;
    };

    // FileLogger.cpp
    #include "Logger.h"

    void CFileLogger::Write()
    {
    char szBuf[CLogger::nameLen];
    ...
    }

    // DnetLogger.cpp
    #include "Logger.h"

    void CDnetLogger::Write()
    {
    char szBuf[CLogger::nameLen];
    ...
    }

    As shown above, CLogger::nameLen was used in serval cpp files.
    I am confused by vc 2005 that one of the cpp files failed to compile.
    It says that CLogger::nameLen is illegal, syntax error.
    Why?
    Allen, Dec 5, 2006
    #1
    1. Advertising

  2. Allen

    Salt_Peter Guest

    Allen wrote:
    > Last time, i post a topic about inline class member declaration in C++.
    > And in VC6, it does not support inline class member.
    >
    > Now I upgrade to VC 2005. But the compiler doesnot completely support
    > it.
    >
    > // Logger.h
    > class CLogger
    > {
    > public:
    > static const int nameLen = 16;
    > };


    What book are you reading that doesn't explain how to define a static
    variable?
    http://www.parashift.com/c -faq-lite/ctors.html

    // Logger.cpp
    const int CLogger::nameLen = 16;

    >
    > // FileLogger.cpp
    > #include "Logger.h"
    >
    > void CFileLogger::Write()
    > {
    > char szBuf[CLogger::nameLen];
    > ...
    > }


    personally, i'ld write the class with a template parameter:

    template< const size_t Size >
    class FileLogger
    {
    char buffer[Size];
    public:
    void write() const ();
    };

    int main()
    {
    FileLogger< 16 > flog;
    }

    // or ...

    #include <vector>
    class FileLogger
    {
    std::vector< char > vc;
    public:
    FileLogger() : vc(16) { }
    FileLogger(size_t sz) : vc(sz) { }
    void write() const ();
    };

    >
    > // DnetLogger.cpp
    > #include "Logger.h"
    >
    > void CDnetLogger::Write()
    > {
    > char szBuf[CLogger::nameLen];
    > ...
    > }
    >
    > As shown above, CLogger::nameLen was used in serval cpp files.
    > I am confused by vc 2005 that one of the cpp files failed to compile.
    > It says that CLogger::nameLen is illegal, syntax error.
    > Why?
    Salt_Peter, Dec 5, 2006
    #2
    1. Advertising

  3. Allen

    Allen Guest

    "Salt_Peter дµÀ£º
    "
    > template< const size_t Size >
    > class FileLogger
    > {
    > char buffer[Size];
    > public:
    > void write() const ();
    > };
    >
    > int main()
    > {
    > FileLogger< 16 > flog;
    > }
    >


    It is not convenient to use in this way.
    Now I find the answer. Because in another header file, there is a
    #define nameLen 16
    macro definition.
    Allen, Dec 6, 2006
    #3
    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. Abhi
    Replies:
    2
    Views:
    734
    E. Robert Tisdale
    Jul 3, 2003
  2. CoolPint
    Replies:
    8
    Views:
    976
    Jeff Schwab
    Dec 14, 2003
  3. Alvin
    Replies:
    7
    Views:
    473
    E. Robert Tisdale
    May 6, 2005
  4. hdixon
    Replies:
    3
    Views:
    643
    hdixon
    Jul 9, 2006
  5. Replies:
    5
    Views:
    341
    Juan T. Llibre
    May 17, 2007
Loading...

Share This Page