非模板从模板继承

时间:2013-12-13 18:11:04

标签: c++ templates inheritance c++11 g++

操作系统: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();
}

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

1 个答案:

答案 0 :(得分:2)

原来我需要的是确保Chain明确调用Peptide构造函数:Chain() : Peptide() {}