Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I have std::vector store these subclasses mixed?

Reply
Thread Tools

Can I have std::vector store these subclasses mixed?

 
 
Eric Lilja
Guest
Posts: n/a
 
      02-26-2005
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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-26-2005
Eric Lilja wrote:

> 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
> };


This enum must be changed whenever you add another derived type. I'd use
typeinfo instead.

>
> Option(const std::string& name)
> :
> m_name(name),
> m_not_set(true) {}
>
> virtual ~Option() {}
>
> T get_value() const
> {
> if(m_not_set)
> throw;


throw what?

>
> 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;
> }
> };


I don't really think you need to derive for each type if you use typeinfo.
I'll present an example of what I mean below.

>
> 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);


Option is a template, not a type. Therefore, you cannot create a vector of
Option pointers.

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


Yes. Make a non-templated base class and derive your Option template from
it. Then store pointers to that base class in your container.

> I'm not very good at templates so I've certainly made a number of
> silly mistakes.



class OptionBase
{
public:
OptionBase(const std::string& name)
: m_name(name),
m_not_set(true)
{
}
virtual std::typeinfo get_type() const = 0;
virtual ~OptionBase() {}

private:
std::string m_name;
bool m_not_set;
};

template <typename T>
class Option : public OptionBase
{
public:
Option(const std::string& name)
: OptionBase(name)
{}

T get_value() const
{
if(m_not_set)
throw SomeException();

return m_value;
}

void set_value(const T& value)
{
m_not_set = false;

m_value = value;
}

virtual std::typeinfo get_type() const
{
return typeid(T);
}

private:
T m_value;
};

//Don't need to derive for each type anymore - a typedef is sufficient
typedef Option<Long> LongOption;
typedef Option<std::string> StringOption;

//...
std::vector<OptionBase*> options(11);
options[0] = new StringOption("Name=");

//And to test for the type:
if (options[0]->get_type() == typeid(std::string))
{
//we have a string
}

Another thing you might be interested in is boost::any. See
http://www.boost.org/doc/html/any.html

 
Reply With Quote
 
 
 
 
Eric Lilja
Guest
Posts: n/a
 
      02-26-2005

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:cvpjvl$jrl$00$(E-Mail Removed)-online.com...
> Eric Lilja wrote:
>
>> 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
>> };

>
> This enum must be changed whenever you add another derived type. I'd use
> typeinfo instead.
>
>>
>> Option(const std::string& name)
>> :
>> m_name(name),
>> m_not_set(true) {}
>>
>> virtual ~Option() {}
>>
>> T get_value() const
>> {
>> if(m_not_set)
>> throw;

>
> throw what?
>
>>
>> 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;
>> }
>> };

>
> I don't really think you need to derive for each type if you use typeinfo.
> I'll present an example of what I mean below.
>
>>
>> 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);

>
> Option is a template, not a type. Therefore, you cannot create a vector of
> Option pointers.
>
>> 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?

>
> Yes. Make a non-templated base class and derive your Option template from
> it. Then store pointers to that base class in your container.
>
>> I'm not very good at templates so I've certainly made a number of
>> silly mistakes.

>
>
> class OptionBase
> {
> public:
> OptionBase(const std::string& name)
> : m_name(name),
> m_not_set(true)
> {
> }
> virtual std::typeinfo get_type() const = 0;
> virtual ~OptionBase() {}
>
> private:
> std::string m_name;
> bool m_not_set;
> };
>
> template <typename T>
> class Option : public OptionBase
> {
> public:
> Option(const std::string& name)
> : OptionBase(name)
> {}
>
> T get_value() const
> {
> if(m_not_set)
> throw SomeException();
>
> return m_value;
> }
>
> void set_value(const T& value)
> {
> m_not_set = false;
>
> m_value = value;
> }
>
> virtual std::typeinfo get_type() const
> {
> return typeid(T);
> }
>
> private:
> T m_value;
> };
>
> //Don't need to derive for each type anymore - a typedef is sufficient
> typedef Option<Long> LongOption;
> typedef Option<std::string> StringOption;
>
> //...
> std::vector<OptionBase*> options(11);
> options[0] = new StringOption("Name=");
>
> //And to test for the type:
> if (options[0]->get_type() == typeid(std::string))
> {
> //we have a string
> }
>
> Another thing you might be interested in is boost::any. See
> http://www.boost.org/doc/html/any.html
>


Very nice, Rolf! I will try out your code as soon as I've showered & shaved,
looks real nice! Thank you for such a detailed response.

/ Eric


 
Reply With Quote
 
Eric Lilja
Guest
Posts: n/a
 
      02-26-2005

"Eric Lilja" <(E-Mail Removed)> wrote in message
news:cvplt7$240$(E-Mail Removed)...
>
> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:cvpjvl$jrl$00$(E-Mail Removed)-online.com...
>> Eric Lilja wrote:
>>
>>> 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
>>> };

>>
>> This enum must be changed whenever you add another derived type. I'd use
>> typeinfo instead.
>>
>>>
>>> Option(const std::string& name)
>>> :
>>> m_name(name),
>>> m_not_set(true) {}
>>>
>>> virtual ~Option() {}
>>>
>>> T get_value() const
>>> {
>>> if(m_not_set)
>>> throw;

>>
>> throw what?
>>
>>>
>>> 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;
>>> }
>>> };

>>
>> I don't really think you need to derive for each type if you use
>> typeinfo.
>> I'll present an example of what I mean below.
>>
>>>
>>> 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);

>>
>> Option is a template, not a type. Therefore, you cannot create a vector
>> of
>> Option pointers.
>>
>>> 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?

>>
>> Yes. Make a non-templated base class and derive your Option template from
>> it. Then store pointers to that base class in your container.
>>
>>> I'm not very good at templates so I've certainly made a number of
>>> silly mistakes.

>>
>>
>> class OptionBase
>> {
>> public:
>> OptionBase(const std::string& name)
>> : m_name(name),
>> m_not_set(true)
>> {
>> }
>> virtual std::typeinfo get_type() const = 0;
>> virtual ~OptionBase() {}
>>
>> private:
>> std::string m_name;
>> bool m_not_set;
>> };
>>
>> template <typename T>
>> class Option : public OptionBase
>> {
>> public:
>> Option(const std::string& name)
>> : OptionBase(name)
>> {}
>>
>> T get_value() const
>> {
>> if(m_not_set)
>> throw SomeException();
>>
>> return m_value;
>> }
>>
>> void set_value(const T& value)
>> {
>> m_not_set = false;
>>
>> m_value = value;
>> }
>>
>> virtual std::typeinfo get_type() const
>> {
>> return typeid(T);
>> }
>>
>> private:
>> T m_value;
>> };
>>
>> //Don't need to derive for each type anymore - a typedef is sufficient
>> typedef Option<Long> LongOption;
>> typedef Option<std::string> StringOption;
>>
>> //...
>> std::vector<OptionBase*> options(11);
>> options[0] = new StringOption("Name=");
>>
>> //And to test for the type:
>> if (options[0]->get_type() == typeid(std::string))
>> {
>> //we have a string
>> }
>>
>> Another thing you might be interested in is boost::any. See
>> http://www.boost.org/doc/html/any.html
>>

>
> Very nice, Rolf! I will try out your code as soon as I've showered &
> shaved, looks real nice! Thank you for such a detailed response.
>
> / Eric
>


Heh, I couldn't wait to try the code out..I'm having some problems...first I
had to change std::typeinfo to std::type_info, guess that was just a simple
typo on your part (happens all the time for me). But I'm still having
problems with the get_type() function. The code is:

class OptionBase
{
public:
OptionBase(const std::string& name)
:
m_name(name),
m_not_set(true) {}

virtual ~OptionBase() {}

virtual std::type_info get_type() const = 0;

private:
std::string m_name;
bool m_not_set;
};

template<typename T>
class Option : public OptionBase
{
public:
Option(const std::string& name)
:
OptionBase(name) {}

virtual std::type_info get_type() const
{
return typeid(T);
}

T get_value() const
{
if(m_not_set)
throw;
return m_value;
}

void set_value(const T& value)
{
m_value = value;
m_not_set = false;
}

private:
T m_value;
};

typedef Option<std::string> StringOption;

int main()
{
std::vector<OptionBase*> options(11);

options[0] = new StringOption("duh");

return 0;
}

and the error is:
/usr/include/c++/3.3.3/typeinfo: In member function `std::type_info
Option<T>::get_type() const [with T = std::string]':
gccmain.cpp:8: instantiated from here
/usr/include/c++/3.3.3/typeinfo:75: error: `std::type_info::type_info(const
std::type_info&)' is private
option.hpp:38: error: within this context
make: *** [gccmain] Error 1

What am I doing wrong? I must say that I have never used type_info before.

/ Eric


 
Reply With Quote
 
Eric Lilja
Guest
Posts: n/a
 
      02-26-2005

"Eric Lilja" <(E-Mail Removed)> wrote in message
news:cvpn3q$2d4$(E-Mail Removed)...
>
> "Eric Lilja" <(E-Mail Removed)> wrote in message
> news:cvplt7$240$(E-Mail Removed)...
>>
>> "Rolf Magnus" <(E-Mail Removed)> wrote in message
>> news:cvpjvl$jrl$00$(E-Mail Removed)-online.com...
>>> Eric Lilja wrote:
>>>
>>>> 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
>>>> };
>>>
>>> This enum must be changed whenever you add another derived type. I'd use
>>> typeinfo instead.
>>>
>>>>
>>>> Option(const std::string& name)
>>>> :
>>>> m_name(name),
>>>> m_not_set(true) {}
>>>>
>>>> virtual ~Option() {}
>>>>
>>>> T get_value() const
>>>> {
>>>> if(m_not_set)
>>>> throw;
>>>
>>> throw what?
>>>
>>>>
>>>> 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;
>>>> }
>>>> };
>>>
>>> I don't really think you need to derive for each type if you use
>>> typeinfo.
>>> I'll present an example of what I mean below.
>>>
>>>>
>>>> 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);
>>>
>>> Option is a template, not a type. Therefore, you cannot create a vector
>>> of
>>> Option pointers.
>>>
>>>> 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?
>>>
>>> Yes. Make a non-templated base class and derive your Option template
>>> from
>>> it. Then store pointers to that base class in your container.
>>>
>>>> I'm not very good at templates so I've certainly made a number of
>>>> silly mistakes.
>>>
>>>
>>> class OptionBase
>>> {
>>> public:
>>> OptionBase(const std::string& name)
>>> : m_name(name),
>>> m_not_set(true)
>>> {
>>> }
>>> virtual std::typeinfo get_type() const = 0;
>>> virtual ~OptionBase() {}
>>>
>>> private:
>>> std::string m_name;
>>> bool m_not_set;
>>> };
>>>
>>> template <typename T>
>>> class Option : public OptionBase
>>> {
>>> public:
>>> Option(const std::string& name)
>>> : OptionBase(name)
>>> {}
>>>
>>> T get_value() const
>>> {
>>> if(m_not_set)
>>> throw SomeException();
>>>
>>> return m_value;
>>> }
>>>
>>> void set_value(const T& value)
>>> {
>>> m_not_set = false;
>>>
>>> m_value = value;
>>> }
>>>
>>> virtual std::typeinfo get_type() const
>>> {
>>> return typeid(T);
>>> }
>>>
>>> private:
>>> T m_value;
>>> };
>>>
>>> //Don't need to derive for each type anymore - a typedef is sufficient
>>> typedef Option<Long> LongOption;
>>> typedef Option<std::string> StringOption;
>>>
>>> //...
>>> std::vector<OptionBase*> options(11);
>>> options[0] = new StringOption("Name=");
>>>
>>> //And to test for the type:
>>> if (options[0]->get_type() == typeid(std::string))
>>> {
>>> //we have a string
>>> }
>>>
>>> Another thing you might be interested in is boost::any. See
>>> http://www.boost.org/doc/html/any.html
>>>

>>
>> Very nice, Rolf! I will try out your code as soon as I've showered &
>> shaved, looks real nice! Thank you for such a detailed response.
>>
>> / Eric
>>

>
> Heh, I couldn't wait to try the code out..I'm having some problems...first
> I had to change std::typeinfo to std::type_info, guess that was just a
> simple typo on your part (happens all the time for me). But I'm still
> having problems with the get_type() function. The code is:
>
> class OptionBase
> {
> public:
> OptionBase(const std::string& name)
> :
> m_name(name),
> m_not_set(true) {}
>
> virtual ~OptionBase() {}
>
> virtual std::type_info get_type() const = 0;
>
> private:
> std::string m_name;
> bool m_not_set;
> };
>
> template<typename T>
> class Option : public OptionBase
> {
> public:
> Option(const std::string& name)
> :
> OptionBase(name) {}
>
> virtual std::type_info get_type() const
> {
> return typeid(T);
> }
>
> T get_value() const
> {
> if(m_not_set)
> throw;
> return m_value;
> }
>
> void set_value(const T& value)
> {
> m_value = value;
> m_not_set = false;
> }
>
> private:
> T m_value;
> };
>
> typedef Option<std::string> StringOption;
>
> int main()
> {
> std::vector<OptionBase*> options(11);
>
> options[0] = new StringOption("duh");
>
> return 0;
> }
>
> and the error is:
> /usr/include/c++/3.3.3/typeinfo: In member function `std::type_info
> Option<T>::get_type() const [with T = std::string]':
> gccmain.cpp:8: instantiated from here
> /usr/include/c++/3.3.3/typeinfo:75: error:
> `std::type_info::type_info(const
> std::type_info&)' is private
> option.hpp:38: error: within this context
> make: *** [gccmain] Error 1
>
> What am I doing wrong? I must say that I have never used type_info before.
>
> / Eric
>


Note: It compiles if I make get_type() a non-virtual member function of the
class Option (i.e., it doesn't appear in class ObjectBase at all). But that
removes the one function that made my OptionBase class an abstract base
class.

/ Eric


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-26-2005
Eric Lilja wrote:

> Heh, I couldn't wait to try the code out..I'm having some problems...first
> I had to change std::typeinfo to std::type_info, guess that was just a
> simple typo on your part (happens all the time for me).


Yes, it was. I haven't used type_info for a while, so I forgot the
underscore

> and the error is:
> /usr/include/c++/3.3.3/typeinfo: In member function `std::type_info
> Option<T>::get_type() const [with T = std::string]':
> gccmain.cpp:8: instantiated from here
> /usr/include/c++/3.3.3/typeinfo:75: error:
> `std::type_info::type_info(const
> std::type_info&)' is private
> option.hpp:38: error: within this context
> make: *** [gccmain] Error 1
>
> What am I doing wrong? I must say that I have never used type_info before.


Again, my error. std::type_info cannot be copied. Try returning it by
reference instead:

virtual const std::type_info& get_type() const
{
return typeid(T);
}

 
Reply With Quote
 
Eric Lilja
Guest
Posts: n/a
 
      02-26-2005

"Rolf Magnus" wrote:
> Eric Lilja wrote:
>
>> Heh, I couldn't wait to try the code out..I'm having some
>> problems...first
>> I had to change std::typeinfo to std::type_info, guess that was just a
>> simple typo on your part (happens all the time for me).

>
> Yes, it was. I haven't used type_info for a while, so I forgot the
> underscore
>
>> and the error is:
>> /usr/include/c++/3.3.3/typeinfo: In member function `std::type_info
>> Option<T>::get_type() const [with T = std::string]':
>> gccmain.cpp:8: instantiated from here
>> /usr/include/c++/3.3.3/typeinfo:75: error:
>> `std::type_info::type_info(const
>> std::type_info&)' is private
>> option.hpp:38: error: within this context
>> make: *** [gccmain] Error 1
>>
>> What am I doing wrong? I must say that I have never used type_info
>> before.

>
> Again, my error. std::type_info cannot be copied. Try returning it by
> reference instead:
>
> virtual const std::type_info& get_type() const
> {
> return typeid(T);
> }
>


Thanks Rolf, I finally made it compile and run without crashing. I had to
make m_not_set a protected (not private) member variable of class OptionBase
and I also removed the value variable from OptionBase. The only thing that
makes the class a bit cumbersome to use is that I have to find the proper
type and cast it to that before being able to call set_value() or
get_value() but I guess there's no way around that.

/ Eric


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ever see these types in a camera store? RichA Digital Photography 6 12-28-2009 09:02 PM
where to store all these digital pictures? aivlis Digital Photography 24 01-14-2006 05:01 PM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
will all these messages cause a problem . I am a new subscriber and my computer is downloading 100,000 messages. Will this cause any kind of a problem with my ability to store other items?? Camille White Camille White Computer Support 9 11-08-2004 01:13 AM
Is it possible to have instance variables in subclasses of builtins? Kenneth McDonald Python 2 06-16-2004 01:02 PM



Advertisments