Where GCC is not understandable in template inheritance (compiler issues)

Source: Internet
Author: User

If a template class inherits from another template class, the members of the inherited template class must be qualified.

(If not qualified, GCC3.4 will appear "Undefined variable error", no problem with VC7 compilation)

The following code is an iterator Treeiterator is a base class and Postorderiter is a subclass

Template <class type>

class Treeiterator {

Public:

Treeiterator ( const binarytree<type>& btree): T (Btree), current (NULL) {}

virtual ~treeiterator () {}

Virtual void                     A () = 0; Place first position as current position

Virtual void                operator+ + () = 0; Current position into a

BOOL   operator+ () const { return current!= NULL;} Determine if it is a valid location in the tree

Const              type& operator() () const; Returns the value that the current position refers to

protected:

Binarytree<type> T;

Const bintreenode<type>* current;

Private:

Treeiterator ( const treeiterator&) {}//Copy Construction

treeiterator& operator= ( const Treeiterator &) const; assigning values

};

Sequence Cursor class

Template <class type>

class Postorderiter: public Treeiterator<type> {

using treeiterator<type>::t;

using treeiterator<type>::current;

in GCC , these two base class variables need to be qualified, otherwise it is impossible to compile (seemingly unreasonable thing, is the C + + standard has changed.) )

// C + + There seems to be no such practice in the standards, and there is no mention in the books related to this .

Public:

Postorderiter ( const binarytree<type>& BT);

~postorderiter () {}

void        A (); Locate the first node in the sequence

void       operator+ + (); Locate the next node in the order

protected:

stack< stknode<type> > St; Recursive work stack for traversal

};

Template <class type>

Postorderiter<type>::P ostorderiter ( const binarytree<type>& BT): treeiterator<type> (BT) {

St. Push (Stknode<type> (T.getroot ()));

In this function, you must use the TREEITERATOR<TYPE>::T qualification to correctly reference T

after looking up the features of the GCC compiler, I discovered some of its practices in dealing with template programming. If you look at the following information, you can see why this problem occurs changes to C + + templates The problems most frequently encountered by C + + programmers are related to the changes that compilers make to follow the C + + standard. GCC 3.4 begins to warn of incorrect C + + usage patterns. In the current GCC 4.0, those warnings turned out to be errors.

One of the more common errors you may encounter is the name lookup problem, which is formally expressed as "undefined on the current scope." Retrieving "name lookup" in the GCC 4.0 documentation will find more information on this issue. The most common four name lookup errors are as follows:

Error

Description

A dependent name in the template class

Names in a function template can be either dependent or have no dependencies. A dependent name is dependent on the template parameter, the name lookup occurs when the template is instantiated, and no dependency name is searched when the template is parsed, a process that takes place before the template is instantiated.

Unqualified names in the template

The name in the template parameter class must be qualified to explicitly indicate that the name is from a template (Foo<t>::marray), or you can use the this pointer to indicate that the name comes from an instance of the template. If you explicitly declare that a name is from a template, the corresponding name lookup occurs when instantiated, otherwise, if you use the this pointer, the name lookup occurs when the template is defined.

Unqualified names in the template superclass

The name defined in the template superclass of the current template must be qualified to come from a superclass. Another approach is to qualify by adding this-> to those names.

Unqualified names in inherited template classes

If a template class inherits from another template class, the members of the inherited template class must be qualified. See the example in Listing 1.


An example of an unqualified name belonging to the inherited template class is shown in Listing 1.

 
 /pre> 
 
 
 
 
 
 
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.