Re: Deriving a regular class from a template?

From: Uwe Schnitker (schnitkerAffenschaukel_at_sigma-c.com)
Date: 04/23/04


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



Relevant Pages

  • Re: operator function
    ... >> of them have anything to do with what the error message seems ... Setting (int h, int m, int s, int TT) ... Now the compiler knows, that it is safe to use that function on a const ... >> operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: problem with linked list
    ... main returns int in C. ... You cannot directly assign to an array in C like this (and this is what your ... compiler is telling you). ... > I am using the Borland c compiler an here is the error message: ...
    (comp.lang.c)
  • Re: Iostream
    ... > it seems when i try to compile this simple code I get an error message i ... then your compiler is installed ... int main ... by putting the 'using' directive or declaration inside the ...
    (comp.lang.cpp)
  • istream_iterator and ostream_iterator problem
    ... I get an error message for the following program by the ... using namespace std; ... int main ...
    (comp.lang.cpp)
  • Re: Problem with Method handling a BaseClass parameter
    ... the base class derives from ... I tell the compiler what function I want not to check ... Overloading is performed at compile-time, ...
    (microsoft.public.dotnet.languages.csharp)