在模板中使用友元类时,未声明的模板类错误

  • 本文关键字:未声明 错误 友元 c++
  • 更新时间 :
  • 英文 :


我试图建立一个自定义的常规网格,所以我有一个模板类的细胞,和一个模板类的网格。但它给了我一个错误时,试图使他们两个在同一个头和都是模板。下面是代码:

#include <list>
using namespace std;
template<typename T>
class Cell{
list<T> points;
public:
friend class Mesh<T>; // Error here "Explicit specialization of undeclared template class"
Cell(): points() {}
void insert(const T &data) { points.push_back(data); }
T *search(const T &data);
bool delete(const T &data);
};

template<typename T>
class Mesh { // Error here "Redefinition of 'Mesh' as different kind of symbol"
float xMin, yMin, xMax, yMax;
float tamaCellX, tamaCellY;
vector<vector<Cell<T>>> mr;
Cell<T> *getCell(float x, float y);
public:
Mesh(int aXMin, int aYMin, int aXMax, int aYMax, int aNDiv);
void insert(float x, float y, const T &data);
T *search(float x, float y, const T &data);
bool delete(float x, float y, const T &data);
};

我做错了什么?

你需要预先声明你正在使用的类模板:

template<typename T>
class Mesh;

Cell定义之前需要。但是,您还需要更改delete成员,因为这是一个保留关键字。你需要包括<vector>,因为你正在使用它。最后,非常不建议使用using namespace std;(因为,当您有大型项目时,您可能会遇到多个库定义自己的列表,向量等)-只需在标准库类之前编写std::

bool delete(float x, float y, const T &data);

delete是c++中的操作符,尝试在两个类中重命名,并且类Mesh在类Cell之后定义,为类Mesh创建标题

最新更新