Re: templates and inheritance
From: James Dennett (jdennett_at_acm.org)
Date: 03/15/04
- Previous message: Greg Comeau: "Re: [C] variable length argument lists"
- In reply to: son, you bore me: "templates and inheritance"
- Next in thread: son, you bore me: "Re: templates and inheritance"
- Reply: son, you bore me: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Mar 2004 23:20:29 -0800
son, you bore me wrote:
> Hello, all.
> I have run into a problem compiling a program in C++. I am trying to perform
> an inheritance off a base template class. The derived class will not create
> any new or modified constructors, just new functions. From googling the
> net, I have learned the derived class does not automatically inherit the
> base class constructors, and attempts to create new functions to work on
> the base class constructors have failed. So, I would like to create a
> constructor in the derived class so I can then use the derived class
> functions on the created objects. Since the derived class constructor are
> not adding or modifying the base class constructor, all I need is the
> derived class constructor to do is pass along any parameters needed by the
> base class constructor. Here is what I have at the moment:
>
> In my NewClass.h file:
>
> #include "BaseClass.h"
>
> template<class Comparable>
> class NewClass : public BaseClass<Comparable>
> {
> public:
> NewClass(Comparable& param);//this is line 8. see errors below
> private:
> };
>
> in my NewClass.cpp file:
>
> #include "NewClass.h"
>
> template<class Comparable>
> NewClass<Comparable>::NewClass(Comparable& param) : BaseClass(param)
> {//no implementation needed. Just passing data}//this is line 8. see errors
>
> in my test program (testClass.cpp):
>
> #include "NewClass.cpp"
> #include <string>
> using namespace std;
>
> int main(){
>
> NewClass<string> test("ZZZZ");//this line 13. see errors
>
> /*blah, blah, and other code */
>
> return 0;
> }
>
> When I compile, I get the following error messages:
>
> testClass.cpp: In function `int main()':
>
> testClass.cpp:13: no matching function for call to
> `NewClass<std::string>::NewClass(const char[2])'
>
> NewClass.h:8: candidates are: NewClass<std::string>::NewClass(const
> NewClass<std::string>&)
>
> NewClass.cpp:8: NewClass<Comparable>::NewClass(Comparable&) [with
> Comparable = std::string]
>
> Did I miss something when I did my inheritance? Did I do my inheritance
> wrong? Is this a potential compiler problem?
>
> Any and all help would be appreciated, as I am a complete novice to C++.
>
> I'm running Linux, and using Kate with Gnu Compiler version 3.2.2
Your problem is not related to templates, nor to inheritance.
It's caused because you're trying to initialize a std::string&
with a char array, which would require a conversion producing
a temporary, and temporaries cannot be used to initialize
non-const references.
The following will not compile:
std::string& r("zz");
If you change the NewClass<Comparable> constructor from taking
Comparable& as an argument to taking Comparable const&, you
might find that this works better.
The following will compile:
std::string const& r("zzconst");
-- James.
- Previous message: Greg Comeau: "Re: [C] variable length argument lists"
- In reply to: son, you bore me: "templates and inheritance"
- Next in thread: son, you bore me: "Re: templates and inheritance"
- Reply: son, you bore me: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|