【问题标题】:Error: Expected template-name before ‘<’ token in C++错误:C++ 中“<”标记之前的预期模板名称
【发布时间】:2016-03-25 03:46:30
【问题描述】:

我在标题中遇到了错误,导致很多其他错误,我真的不知道如何解决它,当我尝试在 main 中声明 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

Liste.h

#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

课程.h

#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

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

【问题讨论】:

  • @JoachimPileborg 这是正确答案。你能打出来吗?

标签: c++ list templates


【解决方案1】:

你有一个循环依赖:Cours.h 依赖于 Liste.h,它依赖于 ListeBase.h,它依赖于 Cours.h,它依赖于。 Liste.h...

我看不出有任何理由将Cours.h 包含在ListeBase.h 中,所以不要将它包含在其中。你不应该包含你实际上不需要的头文件,恕我直言,如果可以避免的话,你根本不应该在头文件中包含头文件,而是按照需要的顺序在源文件中包含所有需要的头文件。

【讨论】:

  • 非常感谢,我已经尝试从 ListeBase.h 中删除 Cours.h 的包含,但由于模板而出现错误,感谢您,我想将它包含在 ListeBase.cxx 和 ii 中!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 2011-08-07
  • 1970-01-01
  • 2021-09-22
  • 2015-01-19
  • 2017-02-19
相关资源
最近更新 更多