Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

c++ - Templated class syntax errors

I have these tree classes.

A ListTraits.h , a List.h and main.cpp

List.h is a templated implementation of list, and it inherits from ListTraits.h

While I believe that my setup for the templated class is correct,

I get several syntax errors, that do not seem like they should be there:

Specifically, for the Listh.h I get:

(line 8) ..

Node<typename>* head;

Error C2143 syntax error: missing ';' before '<' main

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int main

And then a series of other errors but these two make the least sense.

These are my files.

List.h

    #pragma once
#include "ListTraits.h"
//------------ Declarations for List traits used in Test1 in main.cpp

template <typename> class List: public ListTraitsExtended<typename>, 
                                public ListTraits<typename> {
private:
    Node<typename>* head;
public:
    List() {head = NULL;}
    
    unsigned int size() {return 0;}

    ListTraits& insert(const typename& item) {return null;}

    void print() {}
//------------ Declarations for List traits used in Test2 in main.cpp
    const typename* getCurrentElement() const {}

    void advance() {}

    void rewind() {}
};

ListTraits.h

    #pragma once

//------------ Declarations for List traits used in Test1 in main.cpp
template <typename T> class ListTraits
{
    public:
        virtual unsigned int size() = 0;
        virtual ListTraits& insert(const T& item) = 0;
        virtual void print() = 0;
};

//------------ Declarations for List traits used in Test2 in main.cpp
template <typename T> class ListTraitsExtended
{
    public:
        virtual const T* getCurrentElement() const = 0;
        virtual void advance() = 0;
        virtual void rewind() = 0;
};
question from:https://stackoverflow.com/questions/65937942/templated-class-syntax-errors

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

typename is a keyword, not the actual name of a type.

In List.h when you derived from ListTraitsExtended<typename> and ListTraitsExtended<typename>, and use Node<typename>, you are causing syntax errors since these template expect the name of a type to be passed to it, and you're passing it a keyword.

You should give a name to the primary template of List, such as changing it to template <typename T>, and then pass the named type T to each template instead:

template <typename T> class List: public ListTraitsExtended<T>, 
//                 ^ name it here                           ^ use it here
                                  public ListTraits<T> {
//                                                  ^ use it here
private:
    Node<T>* head;
//       ^ and here

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...