vector<Point<T> *> vertices;//记录顶点 vector<Facet<T> *> facets;//记录面 已经声明,但是报错76 14 C:\Users\86139\Documents\c++\实验19.cpp [Error] 'vertices' was not declared in this scope
时间: 2024-02-11 09:09:04 浏览: 31
这个错误提示表明,你在当前作用域中没有声明名为 "vertices" 的变量。你需要在使用变量 "vertices" 前先声明和定义它,例如:
```
#include <vector>
template<typename T>
struct Point {
T x, y, z;
};
template<typename T>
struct Facet {
std::vector<Point<T> *> vertices;
};
int main() {
std::vector<Point<int> *> vertices;
std::vector<Facet<int> *> facets;
// ...
return 0;
}
```
在上面的例子中,我们在 main 函数中定义了 "vertices" 和 "facets" 两个变量,并初始化它们为空向量。在 "Facet" 结构体中,我们使用了 "std::vector" 类型来存储顶点指针,而不是直接使用裸指针,这样可以更好地管理内存。
相关问题
template<typename T> class Polyhedron { public: Polyhedron(const char *path) { } ~Polyhedron() {}private: vector<Point<T> *> vertices;//记录顶点 vector<Facet<T> *> facets;//记录面 }; 为什么编译器报错T没有定义
编译器报错T没有定义,可能是因为在使用 `Polyhedron` 类模板时,没有为模板参数 `T` 指定具体类型,或者在指定类型时发生了错误。在使用类模板时,需要显式地提供模板参数,例如:
```c++
Polyhedron<float> polyhedron("path/to/file");
```
这里将 `T` 指定为 `float` 类型,如果没有为 `T` 指定具体类型,编译器就会报错 T 没有定义。另外,需要确保在当前作用域中,`T` 已经被定义,否则也会报错 T 没有定义。
实现该程序请设计类Polyhedron来保存一个多面体的信息。先通过模板定义一个Point类模板来保存顶点的坐标信息。Point坐标的类型是可变的,比如坐标类型可以是int,可以是float,可以是double等等。再定义一个Facet类来表示每一个面片。面片类也是模板,面片每个顶点的类型是可变的,跟Point模板对应。Facet通过一个vector数组vector<Point<T> *> vertices;记录组成它的几个顶点,每个顶点是一个Point类型的指针。Polyhedron类通过一个vector数组vector<Point<T> *> vertices;记录所有的Point顶点,通过另一个vector数组vector<Facet<T> *> facets;记录所有的Facet面片,每个顶点和每个面片都是一个在堆上new出来的指针。
以下是Polyhedron类的实现:
```c++
template<typename T>
class Point {
public:
Point(T x, T y, T z) : x(x), y(y), z(z) {}
T x, y, z;
};
template<typename T>
class Facet {
public:
vector<Point<T>*> vertices;
};
template<typename T>
class Polyhedron {
public:
vector<Point<T>*> vertices;
vector<Facet<T>*> facets;
void addVertex(Point<T>* vertex) {
vertices.push_back(vertex);
}
void addFacet(Facet<T>* facet) {
facets.push_back(facet);
}
};
```
在上述实现中,Point类和Facet类分别表示多面体中的顶点和面片。Polyhedron类保存所有的顶点和面片,并提供了向其中添加顶点和面片的方法。由于顶点和面片都是在堆上动态分配的,所以需要在程序结束时手动释放它们。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)