哈顿定律:只有一个不可侵犯的法律 KDevelop: http://www.kdevelop.org SuSE: http:/ /www.suse.com Mozilla: http:// www.mozilla.orgThis may be another question having an obvious answer, but I''m not seeingit. I''m trying to create a class that derives fromstd::valarray<std::string>. I don''t need a template, and I haven''t comeacross any examples of a construct like what I show below. Perhapes it''ssimply a bad idea. If there is something fundamentally wrong with thisapproach please let me know.Can anybody tell me if there is a way to get the following to work? I canget 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 -lmmain.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)''--STHHatton''s Law: "There is only One inviolable Law"KDevelop: http://www.kdevelop.org SuSE: http://www.suse.comMozilla: http://www.mozilla.org推荐答案 " Steven T. Hatton" <苏****** @ setidava.kushan.aa>在留言中写道 news:u5 ******************** @ speakeasy.net ..."Steven T. Hatton" <su******@setidava.kushan.aa> wrote in messagenews:u5********************@speakeasy.net...这个可能是另一个有明显答案的问题,但我没有看到它。我正在尝试创建一个派生自 std :: valarray< std :: string>的类。我不需要一个模板,而且我没有来过任何构造的例子,就像我在下面展示的那样。 Perhapes它只是一个坏主意。如果这个方法存在根本性的问题,请告诉我。 有人可以告诉我是否有办法让以下工作?我可以让StringArray类进行编译,但无法正确链接: #include< valarray> #include< string> namespace stringer {使用std :: valarray; 使用std :: string; 类StringArray:public valarray< string> { public: StringArray(const size_t& vsize):valarray< string>(vsize){} }; StringArray stringV(3); //这似乎是失败的} 这就是它失败的地方: 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):在函数中`__static_initialization_and_destruction_ (int,int)''::未定义的引用`std :: basic_string< char,std :: char_traits< char>, std :: allocator< char> > :: _ Rep :: _ S_empty_rep_storage'' main.o(。text + 0x155):在函数`__static_initialization_and_destruction_ (int,int)''::对`std的未定义引用:: basic_string< char,std :: char_traits< char>, std :: allocator< char> > :: _ Rep :: _ S_empty_rep_storage'' main.o(。text + 0x196):函数`__static_initialization_and_destruction_ (int,int)''::对__gnu_cxx的未定义引用:: __ exchange_and_add(int volatile *, int)'' 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>. 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 stringermain.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)'' 您的代码为我编译和链接。我看不出有什么违法行为。 OTOH我不确定这是个好主意。 首先来自您发布的代码 typedef std :: valarray< std :: string> StringArray; 似乎是一个更简单的选项,但也许你有理由这样做 不在发布的代码中。 其次valarray真的是为数字类型设计的,我不认为使用 它带字符串是非法的但它有点奇怪,为什么不 std :: vector< std :: string>? 第三,从标准容器类派生是不赞成的因为 他们缺少虚拟析构函数。这样的东西更安全,可能更接近您的需求 class StringArray { public: ... private: std :: valarray< std :: string>数据; }; johnYour code compiles and links for me. I can''t see anything illegal about it.OTOH I''m not sure its a good idea.Firstly from the code you postedtypedef std::valarray<std::string> StringArray;seems a simpler option, but maybe you have reasons for doing this thataren''t in the posted code.Secondly valarray is really designed for numeric types, I don''t think usingit with strings is illegal but its a bit odd, why notstd::vector<std::string>?Thirdly deriving from standard container classes is frowned upon becausethey lack a virtual destructor. Something like this is safer and may becloser to your needsclass StringArray{public:...private:std::valarray<std::string> data;};john John Harrison写道:John Harrison wrote:您的代码为我编译和链接。 它我看不到任何违法行为。 的确如此。我刚刚构建并安装了gcc(GCC)3.4.0。我一直在使用qmake 来创建Makefile。如果我的设置是 标准,那就行了。但它试图链接一组不同的库。在 特别是-L / usr / lib。我认为gcc优先于那个, 直到我查了一下。哦,然后就是那个带有 automake的aclocal-1.8,并且没有从'make install'获得正确的符号链接!它继续...... / b $ b继续......: - / OTOH我不确定它是个好主意。 首先来自您发布的代码 typedef std :: valarray< std :: string> StringArray; 似乎是一个更简单的选项,但也许你有理由这样做不在发布的代码中。 我最初的动机是在 初始化函数中设置valarray的值,因此需要派生类。 其次valarray真的是为数字类型设计的,我不认为用字符串使用它是非法的但它有点奇怪,为什么不用 std :: vector< std :: string>? 我正在使用琴弦,所以我可以用不同的切片,索引等来玩 时标记各个元素。它是只是一个学习玩具。 第三,从标准容器类派生是不赞成的,因为它们缺少虚拟析构函数。 这很有用。我现在想知道我是否可以在基类上明确地调用 析构函数。嗯.... 这样的东西更安全,可能更贴近你的需求 class StringArray 公共: ... 私人: std :: valarray< std :: string>数据; }; Your code compiles and links for me. I can''t see anything illegal about it.Indeed. I just built and installed gcc (GCC) 3.4.0. I had been using qmaketo create the Makefile. That''s would have worked if my setup werestandard. But it was trying to link against a different set of libs. Inparticular the -L/usr/lib. I thought gcc would take precedence over that,until I looked it up. Oh, and then there''s the aclocal-1.8 that came withautomake, and didn''t get the proper symlink from the `make install''! Itgoes on and on... :-/ OTOH I''m not sure its a good idea. Firstly from the code you posted typedef std::valarray<std::string> StringArray; seems a simpler option, but maybe you have reasons for doing this that aren''t in the posted code.My initial motivation was to set the values of the valarray in aninitialization function, hence the desire for a derived class. Secondly valarray is really designed for numeric types, I don''t think using it with strings is illegal but its a bit odd, why not std::vector<std::string>?I''m using the strings so I can mark the individual elements while I playaround with different slicing, indexing and etc. It''s just a learning toy. Thirdly deriving from standard container classes is frowned upon because they lack a virtual destructor.That''s good to know. I''m now wondering if I can explicitly call thedestructor on the baseclass. Hmmmm.... Something like this is safer and may be closer to your needs class StringArray { public: ... private: std::valarray<std::string> data; }; 这让我大吃一惊。在这种情况下,最好是使用a-a 而不是is-a。谢谢您的帮助。验证它是b 编译的有价值的信息。 - STH 哈顿'法则:只有一个不可侵犯的法律 KDevelop: http ://www.kdevelop.org SuSE: http://www.suse.com Mozilla: http://www.mozilla.orgThat crossed my mind. In this case, it''s probably better to do the has-arather than an is-a. Thanks for the help. The verification that itcompiled was valuable information.--STHHatton''s Law: "There is only One inviolable Law"KDevelop: http://www.kdevelop.org SuSE: http://www.suse.comMozilla: http://www.mozilla.org" Steven T. Hatton" <苏****** @ setidava.kushan.aa>在消息新闻中写道:< u5 ******************** @ speakeasy.net> ..."Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message news:<u5********************@speakeasy.net>...这可能是另一个问题了一个明显的答案,但我没有看到它。我正在尝试创建一个派生自 std :: valarray< std :: string>的类。我不需要一个模板,而且我没有来过任何构造的例子,就像我在下面展示的那样。 Perhapes它只是一个坏主意。如果这个方法存在根本性的问题,请告诉我。 有人可以告诉我是否有办法让以下工作?我可以让StringArray类进行编译,但无法正确链接: #include< valarray> #include< string> namespace stringer {使用std :: valarray; 使用std :: string; 类StringArray:public valarray< string> { public: StringArray(const size_t& vsize):valarray< string>(vsize){} }; StringArray stringV(3); //这似乎是失败的} 这就是它失败的地方: 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):在函数中`__static_initialization_and_destruction_ (int,int)''::未定义引用`std :: basic_string< char,std :: char_traits< char> ;, :未定义引用`__gnu_cxx :: __exchange_and_add(int volatile *,int> std :: allocator< char>> :: _ Rep :: _ S_empty_rep_storage'' main.o(。text + 0x155):在函数`__static_initialization_and_destruction_ (int, int)''::未定义引用`std :: basic_string< char,std :: char_traits< char>, std :: allocator< char>> :: _ Rep :: _ S_empty_rep_storage'' main.o(。text + 0x196):在函数`__static_initialization_and_destruction_ (int,int)'':)'' 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>. 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>, : undefined reference to `__gnu_cxx::__exchange_and_add(int volatile*, int> 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)'':)'' 一可能性是,自从std ::对于优化的数值计算,valarray容器实际上是 ,它有一些特殊限制 如果你试图初始化一个带有 的valarray会导致问题非数字类型(参见第26.3条(标准)。我会建议 使用std :: vector< std :: string> ..那应该绝对有用..我 过去曾经用过它。 HTH,Dave MooreOne possibility is that, since the std::valarray container is reallyfor optimized numerical calculations, it has some special restrictionswhich cause problems if you try to initialize a valarray with anon-numerical type (c.f. sec 26.3 of the standard). I would suggestusing std::vector<std::string> .. that should definitely work .. Ihave used it myself in the past.HTH, Dave Moore 这篇关于从模板中派生常规类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!