blargg said:
First, give us a minimal test program. How does your compiler respond to
the following program?
class Foo {
protected:
static int i;
};
int Foo::i( 123 );
int main() { }
By taking the time to write a minimal test program, in almost all cases
you'll track down the real cause of the bug, and its solution. In other
words, learn to fish.
Well you minimal test program compiles fine.
My error was on a line further down. My code is:
using namespace std;
extern
class SystemName
{
public:
string sys_name;
protected:
SystemName(string sys_name)
: sys_name(sys_name)
{
}
static SystemName sys_type;
}
&systype;
SystemName SystemName::sys_type("basicplatform");
SystemName &systype = SystemName::sys_type;
I am trying to write a single-ton class, that can only be accessed
through a reference, SystemName &systype, instead of a function
like SystemName &SystemName::getInstance(), which is ugly.
The real code is at work and is for a class that exposes
application-global settings as public data members initialized
from the application's configuration file. I think a single-ton
class is appropriate for such a case, and I want the users of
the configuration class to be able to access just the reference.
The error is obviously on the last line where the reference is
initialized with the protected member, but still in the compiler
output messages the first error is listed at the previous line,
with the definition of the static member, making me believe the
compiler could not define the protected static member.
I do not know why g++ reports the error as if on a previous line,
even in version 4.2.4
Thank you,
Timothy Madden
/*
* POSIX version test
*
* Program to test POSIX version, if running on a POSIX system,
* at compile and run time, and display values found. Returns
* 0 to indicate success if on a POSIX system, non-zero otherwise.
*/
#define _POSIX_SOURCE 199309
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#define STRINGIZE(a) #a
#define MAKE_STRING2(a) STRINGIZE(a)
#define MAKE_STRING(a) MAKE_STRING2(a)
using namespace std;
extern
class SystemName
{
public:
string sys_name;
protected:
SystemName(string sys_name)
: sys_name(sys_name)
{
}
static SystemName sys_type;
}
&systype;
SystemName SystemName::sys_type("basicplatform");
SystemName &systype = SystemName::sys_type;
int main()
try
{
#ifdef _POSIX_VERSION
cout << "Built for POSIX ";
cout << MAKE_STRING(_POSIX_VERSION);
cout << endl;
if (sysconf(_SC_VERSION) != -1)
{
cout << "Running on POSIX ";
cout << sysconf(_SC_VERSION);
cout << endl;
return EXIT_SUCCESS;
}
else
{
cout << "Not running on a POSIX system.\n";
return EXIT_FAILURE;
}
#else
cout << "Not built on a POSIX system.\n";
return EXIT_FAILURE;
#endif
}
catch (exception &e)
{
cerr << e.what();
cerr << endl;
return EXIT_FAILURE;
}
catch (...)
{
cerr << "Application error.\n";
return EXIT_FAILURE;
}