Re: Deriving a regular class from a template?
From: Uwe Schnitker (schnitkerAffenschaukel_at_sigma-c.com)
Date: 04/23/04
- Next message: Calum: "Re: OOP Language for OS Development"
- Previous message: Karl Heinz Buchegger: "Re: C++ Project Files?????"
- In reply to: Steven T. Hatton: "Deriving a regular class from a template?"
- Next in thread: Dave Moore: "Re: Deriving a regular class from a template?"
- Reply: Dave Moore: "Re: Deriving a regular class from a template?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 23 Apr 2004 02:50:56 -0700
"Steven T. Hatton" <susudata@setidava.kushan.aa> wrote in message news:<u5adnYHwA7ipGBXdRVn-jw@speakeasy.net>...
> This may be another question having an obvious answer, but I'm not seeing
> it. I'm trying to create a class that derives from
> std::valarray<std::string>.
This is a bad idea, for at least three reasons:
i) valarray is a special container designed for numerical applications,
so there is propbably no point in using it with string
ii) IIRC there are some restrictions in the standard for using valarray,
to the consequence that using it with string yields undefined
behaviour
iii) deriving from classes which are not designed as base classes, like
the standard library containers, is considered bad practice in C++,
e.g. the lack of a virtual destructor may prove fatal
To your more general question if one can derive a concrete class from
a template instantiation: Yes, that's possible.
If you had used vector instead of valarray, your example would be correct,
even if bad style, for reason iii).
And if you look at your error message, you might see that the code as written
compiled fine. The error message refers to a linker error. You are probable
not linking against the C++ runtime library, or maybe you are linking against
a wrong one.
To solve this problem, check your documentation or ask in a newsgroup topical
to your compiler system.
> I don't need a template, and I haven't come
> across any examples of a construct like what I show below. Perhapes it's
> simply a bad idea. If there is something fundamentally wrong with this
> approach please let me know.
>
> Can anybody tell me if there is a way to get the following to work? I can
> get the class StringArray to compile, but it fails to link correctly:
>
> #include <valarray>
> #include <string>
>
> namespace stringer{
> using std::valarray;
> using std::string;
>
> class StringArray:public valarray<string> {
> public:
> StringArray(const size_t& vsize):valarray<string>(vsize){}
> };
>
> StringArray stringV(3); // this is what seems to be failing
> }
>
> This is the point where it fails:
>
> g++ -o stringer main.o -L/usr/lib/ -L/usr/lib/qt3/lib/ -L/usr/X11R6/lib/
> -lqt -lXext -lX11 -lm
> main.o(.text+0xd7): In function `__static_initialization_and_destruction_
> (int, int)':
> : undefined reference to `std::basic_string<char, std::char_traits<char>,
> std::allocator<char> >::_Rep::_S_empty_rep_storage'
> main.o(.text+0x155): In function `__static_initialization_and_destruction_
> (int, int)':
> : undefined reference to `std::basic_string<char, std::char_traits<char>,
> std::allocator<char> >::_Rep::_S_empty_rep_storage'
> main.o(.text+0x196): In function `__static_initialization_and_destruction_
> (int, int)':
> : undefined reference to `__gnu_cxx::__exchange_and_add(int volatile*, int)'
- Next message: Calum: "Re: OOP Language for OS Development"
- Previous message: Karl Heinz Buchegger: "Re: C++ Project Files?????"
- In reply to: Steven T. Hatton: "Deriving a regular class from a template?"
- Next in thread: Dave Moore: "Re: Deriving a regular class from a template?"
- Reply: Dave Moore: "Re: Deriving a regular class from a template?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|