E
Eric Lilja
Hello, in my program I am reading a text file that has format:
option=value
There are several types of options and they expect values of different types
(mostly strings or integers). I made a templated base class Option and had
my different options derive from Option instantiated for their type (i.e.,
class LongOption inherits publicly from Option<long>). I selected this
approach because I wanted a uniform interface for obtaining the type and
value of a given option. I came up with the following class tree for testing
purposes:
template <typename T>
class Option
{
public:
enum OptionType
{
LONG,
STRING
};
Option(const std::string& name)
:
m_name(name),
m_not_set(true) {}
virtual ~Option() {}
T get_value() const
{
if(m_not_set)
throw;
return m_value;
}
void set_value(const T& value)
{
m_not_set = false;
m_value = value;
}
virtual OptionType get_type() const = 0;
private:
std::string m_name;
bool m_not_set;
T m_value;
};
class LongOption : public Option<long>
{
public:
LongOption(const std::string& name)
:
Option<long>(name) {}
virtual OptionType get_type() const
{
return STRING;
}
};
class StringOption : public Option<std::string>
{
public:
StringOption(const std::string& name)
:
Option<std::string>(name) {}
virtual OptionType get_type() const
{
return LONG;
}
};
That seems to compile, but I cannot do:
std::vector<Option*> options(11);
options[0] = new StringOption("Name=");
The compiler complains:
$ make gccmain
g++ gccmain.cpp -o gccmain
gccmain.cpp: In function `int main()':
gccmain.cpp:6: error: use of class template `template<class T> class Option'
as expression
gccmain.cpp:6: error: parse error before `>' token
gccmain.cpp:8: error: `options' undeclared (first use this function)
gccmain.cpp:8: error: (Each undeclared identifier is reported only once for
each function it appears in.)
make: *** [gccmain] Error 1
Not being able to store my different options in the same container makes
reading the file with options a lot messier...are their any ways to solve
this? I'm not very good at templates so I've certainly made a number of
silly mistakes.
/ Eric
option=value
There are several types of options and they expect values of different types
(mostly strings or integers). I made a templated base class Option and had
my different options derive from Option instantiated for their type (i.e.,
class LongOption inherits publicly from Option<long>). I selected this
approach because I wanted a uniform interface for obtaining the type and
value of a given option. I came up with the following class tree for testing
purposes:
template <typename T>
class Option
{
public:
enum OptionType
{
LONG,
STRING
};
Option(const std::string& name)
:
m_name(name),
m_not_set(true) {}
virtual ~Option() {}
T get_value() const
{
if(m_not_set)
throw;
return m_value;
}
void set_value(const T& value)
{
m_not_set = false;
m_value = value;
}
virtual OptionType get_type() const = 0;
private:
std::string m_name;
bool m_not_set;
T m_value;
};
class LongOption : public Option<long>
{
public:
LongOption(const std::string& name)
:
Option<long>(name) {}
virtual OptionType get_type() const
{
return STRING;
}
};
class StringOption : public Option<std::string>
{
public:
StringOption(const std::string& name)
:
Option<std::string>(name) {}
virtual OptionType get_type() const
{
return LONG;
}
};
That seems to compile, but I cannot do:
std::vector<Option*> options(11);
options[0] = new StringOption("Name=");
The compiler complains:
$ make gccmain
g++ gccmain.cpp -o gccmain
gccmain.cpp: In function `int main()':
gccmain.cpp:6: error: use of class template `template<class T> class Option'
as expression
gccmain.cpp:6: error: parse error before `>' token
gccmain.cpp:8: error: `options' undeclared (first use this function)
gccmain.cpp:8: error: (Each undeclared identifier is reported only once for
each function it appears in.)
make: *** [gccmain] Error 1
Not being able to store my different options in the same container makes
reading the file with options a lot messier...are their any ways to solve
this? I'm not very good at templates so I've certainly made a number of
silly mistakes.
/ Eric