错误:C++中"<"标记之前的预期模板名称

Error: Expected template-name before ‘<’ token in C++

本文关键字:C++ lt 错误      更新时间:2023-10-16

我在标题中遇到了错误,导致很多其他错误,我真的不知道如何解决它,当我尝试声明一个列表 ListeTriee 时,错误就开始了,在此之前我没有任何问题。

错误

In file included from Cours.h:10:0,
                 from ListeBase.h:13,
                 from ListeBase.cxx:1:
Liste.h:12:51: error: expected template-name before ‘<’ token
 template<typename T> class Liste: public ListeBase<T>
                                                   ^
Liste.h:12:51: error: expected ‘{’ before ‘<’ token
Liste.h:12:51: error: expected unqualified-id before ‘<’ token
In file included from ListeBase.h:13:0,
                 from ListeBase.cxx:1:
Cours.h:19:20: error: field ‘groupes’ has incomplete type
         Liste<int> groupes;
                    ^
Cours.h: In member function ‘void Cours::insererGroupe(int)’:
Cours.h:28:13: error: ‘groupes’ was not declared in this scope
             groupes.insere(val);

这些文件是:

ListeBase.h

#ifndef LISTE_BASE
#define LISTE_BASE
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
#include "Timing.h"
#include "Professeur.h"
#include "Groupe.h"
#include "Local.h"
#include "Cours.h"
template<typename T> struct Cellule
{
    T valeur ;
    Cellule<T> *suivant ;
    Cellule(T v, Cellule<T> *s) : valeur(v), suivant(s) {};
};
template<typename T> class Iterateur;
template<typename T> class ListeBase
{
    protected:
        Cellule<T> *pTete ;
    public:
        ListeBase();
        ~ListeBase();
        bool estVide() const;
        int getNombreElements() const;
        void Affiche() const;
        virtual T* insere(const T & val)=0;
                friend class Iterateur<T>;
};
#endif

利斯特·

#ifndef LISTE
#define LISTE
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
#include "ListeBase.h"
#include "Timing.h"
template<typename T> class Liste: public ListeBase<T>
{
    public:
        T* insere(const T & val);
};
#endif

库尔斯·

#ifndef COURS
#define COURS
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#include "Liste.h"
#include "Event.h"
#include "Professeur.h"
#include "Groupe.h"
class Cours: public Event
{
    private:
        char* professeur;
        Liste<int> groupes;
    public:
        Cours();
        Cours(const Cours &);
        ~Cours();
        const char* getProfesseur() const;
        void setProfesseur(Professeur &);
        void insererGroupe(int val)
        {
            groupes.insere(val);
        }
        void Affiche();
};
#endif

谢谢,如果您需要更多详细信息,请询问。

你有一个循环依赖关系:Cours.h取决于Liste.h取决于ListeBase.h取决于Cours.h依赖。 Liste.h......

我认为没有任何理由将Cours.h包含在ListeBase.h中,因此干脆不要将其包含在那里。您不应该包含您实际上不需要的头文件,恕我直言,如果可以避免,您根本不应该在头文件中包含头文件,而是按照所需的顺序将所有需要的头文件包含在源文件中。