wtf...? deriving from template

This is a discussion on wtf...? deriving from template within the C++ Programming forums, part of the General Programming Boards category; Code: template <class T> class B { protected: T *p; // even if replacing T with int doesnt help }; ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    wtf...? deriving from template

    Code:
    template <class T> class B {
    protected:
      T *p;
      // even if replacing T with int doesnt help
    };
    
    template <class T> class D : public B<T> {
    public:
      D()
      {
        p = NULL; // error ?!?!?!?!?!
      }
    };
    why does it say p is undefined?

    (using gcc)
    is it a syntatic problem?
    signature under construction

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Works for me using MS.Net 2003.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    yes, that code works for me too on ms-vc6.0
    but it fails to compile with g++ >_<
    error:
    test.cpp:18: error: `p' undeclared (first use this function)
    test.cpp:18: error: (Each undeclared identifier is reported only once for each function it appears in.)

    [edit]
    so my first guess would be that its a syntatic problem and the ms compilers allow that syntax while the c++ standard does not... i might be wrong though

    [edit2]
    the funny thing is that when replacing B<T> with e.g. B<int> in line
    template <class T> class D : public B<T>
    it compiles...
    Last edited by Raven Arkadon; 06-27-2005 at 03:18 PM.
    signature under construction

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    oh well...

    i found this "fix"
    but would still be nice if anyone could tell me what the problem is,
    btw: gcc - v yields
    gcc version 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)

    Code:
    template <class T>
    class D : public B<T> {
    public:
      using B<T>::p; // !!!
    
      D()
      {
        p = NULL;
      }
    };
    signature under construction

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    it could be a bug. I had a problem when overloading += operator recently in gcc
    template/operator overloading ambigous on gcc
    but probaly you're getting that error because you're acessing the variable from a constructor, which shouldn't hold any problem. Make a method and try to use the variable in it, to se what happens

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    methods yield the same error.

    so its either a bug, or in some documents it states that it has to be done that way... strange

    well i could try a newer version of gcc (even though there are only 2-3 newer versions)
    signature under construction

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why does it say p is undefined?
    Because it is undefined. p doesn't depend on the template parameter T for class D, but class B does. The standard says that dependent base classes aren't searched when looking up non-dependent names. The solution is either to qualify p with B<T>:
    Code:
    template <class T> class B {
    protected:
      T *p;
      // even if replacing T with int doesnt help
    };
    
    template <class T> class D : public B<T> {
    public:
      D()
      {
        B<T>::p = NULL; // error ?!?!?!?!?!
      }
    };
    Or this:
    Code:
    template <class T> class B {
    protected:
      T *p;
      // even if replacing T with int doesnt help
    };
    
    template <class T> class D : public B<T> {
    public:
      D()
      {
        this->p = NULL; // error ?!?!?!?!?!
      }
    };
    Assuming you don't include a header that defines NULL, you also have an error there.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    hehe, so
    adding
    using B<T>::p (posted earlier in the thread)
    to my D class was just the right thing to do

    thxalot! well my other guess was also right: there is some standard which states that this shouldnt compile
    and it also shows that m$ doesnt follow it...
    signature under construction

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >there is some standard which states that this shouldnt compile
    Yes

    >and it also shows that m$ doesnt follow it...
    Despite your apparent attempt to bash Microsoft, you won't find a compiler that completely conforms to the C++ standard, but Microsoft's is one of the few that come close.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    now that i have fedora core 3 running, let me have the fun bashing ms
    and ms needs to bashed for many reasons (like threatening asian governments who use linux)
    and i don't wanna know what happens at the vote this wednesday when they decide about software patents in europe >_<

    edit:
    oops, this wednesday is tomorrow, i meant next wednesday
    Last edited by Raven Arkadon; 06-27-2005 at 08:08 PM.
    signature under construction

  11. #11
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    that code to a programmer isn't ambiguous, but if the standard says so, then we'll have to carry the book around whenever we want to use more complex syntax...

    it's funny to see that the website for c/c++ standard, doesn't use standard html
    Last edited by xErath; 06-27-2005 at 09:12 PM.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What does the C++ standard have to do with the HTML standard?

    Just remember that it's not a good idea to initialize base members in the derived constructor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21