C
crjjrc
Hi. I've got a templated class Field<T>. The T ultimately resolves
to the various primitives like float and unsigned char. For each T I
know I will need, I define a static member variable inside Field whose
value is specific to the type. I define the constant for each type in
the definition file. The contents for the Field-related files are:
field.h
-------------------------------
#ifndef FIELD_H
#define FIELD_H
template<class T>
class Field {
public:
static const int moo;
};
#endif
field.cpp
-------------------------------
#include "field.h"
template<> const int Field<char>::moo = 7;
Okay, now when I try to reference this const member variables in a
switch statement, g++ gives me an error. The switch statement is
called from within a static function of another class, called
VolumeBase:
volumebase.h
-------------------------------
#include "field.h"
class VolumeBase {
public:
static void volfunc();
};
volumebase.cpp
-------------------------------
#include <iostream>
#include "volumebase.h"
void VolumeBase::volfunc() {
int val = 7;
switch (val) {
case Field<char>::moo:
std::cout << "in char" << std::endl;
break;
default:
std::cout << "failed" << std::endl;
break;
}
}
And when I try to compile all these...
$ g++ -c volumebase.cpp field.cpp
volumebase.cpp: In static member function `static void
VolumeBase::volfunc()':
volumebase.cpp:8: error: `Field<char>::moo' cannot appear in a
constant-expression
I would think that Field<char> could be resolved at this point and
that it would be constant, but this error message indicates that it is
not.
Any ideas? Help is appreciated.
- Chris
to the various primitives like float and unsigned char. For each T I
know I will need, I define a static member variable inside Field whose
value is specific to the type. I define the constant for each type in
the definition file. The contents for the Field-related files are:
field.h
-------------------------------
#ifndef FIELD_H
#define FIELD_H
template<class T>
class Field {
public:
static const int moo;
};
#endif
field.cpp
-------------------------------
#include "field.h"
template<> const int Field<char>::moo = 7;
Okay, now when I try to reference this const member variables in a
switch statement, g++ gives me an error. The switch statement is
called from within a static function of another class, called
VolumeBase:
volumebase.h
-------------------------------
#include "field.h"
class VolumeBase {
public:
static void volfunc();
};
volumebase.cpp
-------------------------------
#include <iostream>
#include "volumebase.h"
void VolumeBase::volfunc() {
int val = 7;
switch (val) {
case Field<char>::moo:
std::cout << "in char" << std::endl;
break;
default:
std::cout << "failed" << std::endl;
break;
}
}
And when I try to compile all these...
$ g++ -c volumebase.cpp field.cpp
volumebase.cpp: In static member function `static void
VolumeBase::volfunc()':
volumebase.cpp:8: error: `Field<char>::moo' cannot appear in a
constant-expression
I would think that Field<char> could be resolved at this point and
that it would be constant, but this error message indicates that it is
not.
Any ideas? Help is appreciated.
- Chris