Inline class members used in vc 2005

A

Allen

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?
 
S

Salt_Peter

Allen said:
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?
 
A

Allen

"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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top