Re: templates and inheritance

From: James Dennett (jdennett_at_acm.org)
Date: 03/15/04

  • Next message: Nai Namae: "Re: Java or C++?!!"
    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.


  • Next message: Nai Namae: "Re: Java or C++?!!"

    Relevant Pages

    • Re: getting used to Java - question about "style"
      ... > I am reading a book that a collegue gave me called "The Elements of Java ... > before executing the derived class constructors. ... > constructor." ... void print() { ...
      (comp.lang.java.programmer)
    • :: Scope qualifier misunderstanding.
      ... arguments from the derived constructor). ... I was hoping this would set the values in the derived class using the ... (inherits base2). ... // Base 2 declaration, ...
      (comp.lang.cpp)
    • Re: What replace "this" in static class?
      ... Originally, your class, as a derived class of QuickFix.Application, ... > function to do the constructor job. ... > This class inherit from a QuickFix.Application class. ... > And previously I was using "this" keyword because the class inherit ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Constructor inheritance (or lack thereof)
      ... > constructor" to mean a constructor which is supplied when no other ones ... The OP realised that constructor inheritance was at least not ... because the class you called Base doesn't have a parameterless ... Derived class *does* have a constructor which takes an int parameter. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: static constructor in derived class not being called
      ... Before the call to MyDerived.DoSomething, the CLR loads MyDerived and ... constructor. ... > I have the following base class and derived class. ... > public class MyBase ...
      (microsoft.public.dotnet.languages.csharp)