【发布时间】:2013-12-13 18:11:03
【问题描述】:
操作系统:Linux Mint 15 肉桂 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++ 的错误。这篇文章最接近,但并没有完全解决这个问题。
我有三个班级,VertexSet、Peptide 和 Chain。 VertexSet是模板类,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() {} -
试过了。必须等待 8 小时才能发布我自己的答案 =/。 StackOverflow 是我的teddy bear 有点有趣
-
您应该了解SSCCE:简短、自包含、可编译的示例(或在您的情况下不可编译)。通常,通过将问题减少到最简单的要求,解决方案会自行弹出……那问题会更有趣;因为您提供了一堵文字墙(这么长!),但未能提供诊断中提到的
bChain.h...
标签: c++ templates inheritance c++11 g++