2

I am trying to create a vector of custom objects defined in a header file and then initialize them in the actual cpp file. I'm getting the following errors in Visual Studio:

error C2976: 'std::vector' : too few template arguments
error C2065: 'Particle' : undeclared identifier
error C2059: syntax error : '>'

In the code below, the vector is defined in Explosion.h.

Particle.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() {
}

Explosion.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() {
}

I'm sure there is something fundamentally wrong here since C++ is fairly new to me.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Mitch
  • 25
  • 6

1 Answers1

7

You need to tell Explosion.h what a Particle is.

In this case, Explosion.h is using Particle*, so a forward declartion will suffice.

Explosion.h

class Particle; // forward declaration of Particle

class Explosion {
// ...
};

You could also simply #include "Particle.h, however as your projects increase using forward declarations (instead of direct includes) can significantly reduce your build times.

Chad
  • 18,706
  • 4
  • 46
  • 63
  • 2
    Is the forward declaration necessary? It just looks like `Explosion.h` needs to `#include "Particle.h"`. – ssell Dec 16 '13 at 20:21
  • 5
    Either way would work. Forward declarations will be preferred as the code base grows, as reducing the number of direct includes can significantly reduce build times. – Chad Dec 16 '13 at 20:22
  • 1
    Also suggest him to avoid C raw pointers, new and delete; in favor of leak-free smart pointers – Manu343726 Dec 16 '13 at 20:23
  • 3
    @ssell: You might instead say, "Is `including` the file necessary? It looks like `Explosion.h` just needs to forward declare the class." – Bill Dec 16 '13 at 20:23
  • @Chad - but the only place that includes `explosion.h` has `#include "particle.h"` immediately preceding it...? – Roddy Dec 16 '13 at 20:30
  • @Manu343726 As I am new to this, what do you mean by "leak-free smart pointers"? Maybe a link to some documentation? – Mitch Dec 16 '13 at 20:30
  • @Mitch (In addition to wikipedia's link): http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one – Manu343726 Dec 16 '13 at 20:41
  • @Manu343726 from what I've read, a smart pointer can not be used with a std::vector. What do you suggest? – Mitch Dec 16 '13 at 20:48
  • 1
    @Mitch: I suggest you read something more accurate. C++11's standard smart pointers can be stored in containers quite happily. (That wasn't the case before 2011, but hopefully you're learning modern C++). – Mike Seymour Dec 16 '13 at 20:55