自定义对象的矢量
Vector of Custom Objects
我正在尝试创建一个在头文件中定义的自定义对象向量,然后在实际的cpp文件中初始化它们。我在Visual Studio中收到以下错误:
error C2976: 'std::vector' : too few template arguments
error C2065: 'Particle' : undeclared identifier
error C2059: syntax error : '>'
在下面的代码中,矢量在Exploration.h.中定义
粒子.h:
#pragma once
class Particle : public sf::CircleShape {
public:
float speed;
bool alive;
float vx;
float vy;
Particle(float x, float y, float vx, float vy, sf::Color color);
~Particle();
};
Particle.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
Particle::Particle(float x, float y, float vx, float vy, sf::Color color) {
// Inherited
this->setPosition(x, y);
this->setRadius(5);
this->setFillColor(color);
// Player Defined Variables
this->speed = (float).05;
this->alive = true;
this->vx = vx;
this->vy = vy;
}
Particle::~Particle() {
}
爆炸.h:
static const int NUM_PARTICLES = 6;
#pragma once
class Explosion {
public:
std::vector<Particle*> particles;
bool alive;
Explosion();
~Explosion();
};
Explosion.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
#include "Explosion.h"
Explosion::Explosion() {
this->alive = true;
// Add Particles to vector
for (int i = 0; i < NUM_PARTICLES; i++) {
this->particles.push_back(new Particle(0, 0, 0, 0, sf::Color::Red));
}
}
Explosion::~Explosion() {
}
我确信这里有一些根本性的错误,因为C++对我来说是相当新的。
您需要告诉Explosion.h
什么是Particle
。
在这种情况下,Explosion.h
使用Particle*
,因此前向解密就足够了。
爆炸。h
class Particle; // forward declaration of Particle
class Explosion {
// ...
};
您也可以简单地使用#include "Particle.h
,但是随着项目的增加,使用正向声明(而不是直接包含)可以显著减少构建时间。
相关文章:
- 自定义对象的dlib序列化在gcc中失败
- 如何将带有自定义对象的容器从C++传递到QML
- 将 exprtk 与自定义类的对象一起使用
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- 如何实现自定义匹配器以检查 Catch2 中的对象相等性
- 如何使用Q_PROPERTY公开自定义对象列表
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 从自定义类获取对象向量中的 max 元素
- 如何将来自 Boost.Python 的map_indexing_suite与自定义而不是标准对象一起使用?
- 如何在自定义对象的<<运算符中添加自定义前缀
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 如何从 cpp 中的函数返回自定义类对象?
- 如何为自定义模板对象创建专门的函数模板
- 带有自定义deleter的std::unique_ptr对象的大小(一个由ref捕获的lambda)
- 基于3个对象的自定义排序
- 如何键入用于const对象的自定义io操纵器
- 指向临时对象的自定义迭代器(延迟加载)
- 将自定义对象作为参数从目标 C 传递到 C++
- 从 Arduino 中的自定义C++库构造对象时出错
- 从链表访问对象(自定义实现)