操作系统:Linux Mint 15 Cinnamon
g++:4.7.3(Ubuntu / Linaro 4.7.3-1ubuntu1)

从g++中的非模板类继承模板类时,出现以下错误:

In file included from common/bChain.h:13:0,
                 from common/bProtein.h:12,
                 from common/bProtein.cpp:12:
common/bPeptide.h:27:31: error: expected template-name before ‘<’ token
common/bPeptide.h:27:31: error: expected ‘{’ before ‘<’ token
common/bPeptide.h:27:31: error: expected unqualified-id before ‘<’ token

这里提出了几个类似的问题:
    http://stackoverflow.com/questions/10265861/error-expected-class-name-before-token-with-templates (template inheriting from template)
    http://stackoverflow.com/questions/10548742/c-inheritance-and-templates-not-compiling (uninitialized members in base class)

但是他们每个人回答一个不同的问题。 This post建议以下内容应与VisualStudio一起使用,我认为g++出错。该帖子最接近,但并未完全解决该问题。

我有三个类,VertexSetPeptideChainVertexSet是模板类,Peptide是从VertexSet继承的非模板类,Chain是从Peptide继承的:
VertexSet.h:
#ifndef B_VERTEXSET_H
#define B_VERTEXSET_H

//////////////// STL
#include <vector>

//////////////// project
#include <Vertex.h>
#include <Spatial.h>
#include <Pool.h> // for pool resources

namespace griddock { template <typename T> class VertexSet; };

/** @brief \c VertexSet \c
 *  @author     Stephen J. Bush
 *  @copyright  Creative Commons Attribution Non-Commercial License V2.0
 *  @copyright  Creative Commons Attribution ShareAlike License V3.0
 *  @par
 */
template <typename T>
class griddock::VertexSet
   :  virtual public Spatial
{
   /////////////////////////////////////////////////////////////////////
   //       Tor
protected:
   VertexSet();
   VertexSet(const VertexSet &rhs);
public:
   ~VertexSet();

   /* ... more functions here ...*/
};

#define GVXS   griddock::VertexSet<T>

////////////////////////////////////////////////////////////////////////
///@name          Tor
///@{
template <typename T>
GVXS::VertexSet()
   :  vertex_(),
      min_(),
      max_()
{
}

template <typename T>
GVXS::VertexSet(const VertexSet &rhs)
   :  vertex_(rhs.vertex_),
      min_(),
      max_()
{
}


template <typename T>
GVXS::~VertexSet()
{
   clear();
}

///@}
//                .
////////////////////////////////////////////////////////////////////////

#undef GVXS

#endif
Peptide.h
#ifndef B_PEPTIDE_H
#define B_PEPTIDE_H

//////////////// STL
#include <vector>
#include <string>

//////////////// project
#include <Pool.h>
#include <Residue.h>
#include <Spatial.h>
#include <Vertex.h>
#include <VertexSet.h>
#include <File.h>
#include <IPeptide.h>

namespace griddock { class Peptide; };


/** @brief \c Peptide \c implements a data structure to store protein residues
 *  @author     Stephen J. Bush
 *  @copyright  Creative Commons Attribution Non-Commercial License V2.0
 *  @copyright  Creative Commons Attribution ShareAlike License V3.0
 *  @par
 */
class griddock::Peptide
   :  virtual public VertexSet<Residue>,
      virtual public IPeptide
{
   /////////////////////////////////////////////////////////////////////
   //       Tor
public:
   Peptide();
   Peptide(const Peptide &peptide);
   ~Peptide();

   /* ...other functions here...*/

};

#endif
Peptide.cpp
//////////////// header
#include <Peptide.h>

//////////////// STL
#include <vector>
#include <string>

//////////////// project
#include <Residue.h>
#include <File.h>

using namespace std;
using namespace griddock;

#define GPEP   Peptide

////////////////////////////////////////////////////////////////////////
///@name          Tor
///@{
GPEP::Peptide()
   :  VertexSet<Residue>()
{
}

GPEP::Peptide(const Peptide &peptide)
   :  VertexSet<Residue>(),
      vertex_(peptide.vertex_),
      chainid_(peptide.chainid_)
{
}


GPEP::~Peptide()
{
   clear();
}

///@}
//                .
////////////////////////////////////////////////////////////////////////

最佳答案

事实证明,我需要做的就是确保Chain明确称为Peptide构造函数:Chain() : Peptide() {}

10-02 12:44