Reputation: 1706
I have defined a particle class Particle in a .hpp file like this
#ifndef PARTICLE_HPP
#define PARTICLE_HPP
#include <glm/glm.hpp>
#include <GL/glew.h>
#include <GL/gl.h>
#include <vector>
struct Particle
{
Particle()
: m_Position(0.0f)
, m_Velocity(0.0f)
, m_Color(1.0f)
, m_fRotate(0.0f)
, m_fAge(0.0f)
, m_fLifeSpan(0.0f)
, m_is_alive(false)
{}
public:
glm::vec3 m_Position; // Center point of particle
glm::vec3 m_Velocity; // Current particle velocity
glm::vec4 m_Color; // Particle color
GLfloat m_fRotate; // Rotate the particle the center
GLfloat m_fSize; // Size of the particle
GLfloat m_fAge;
GLfloat m_fLifeSpan;
bool m_is_alive;
};
In another .cpp file i declared Particle in typedef namespace. What i am trying to do is instantiate Particle struct and push them in a vector. Since i have an empty constructor i believe instantiating with just Particle() should work. However, i could not do it because the compile argues there is 'incomplete type not allowed' error in the bolded line. particles.push_back(Particle());
#include "Particle.hpp"
#include <stdlib.h> //malloc
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include<vector>
typedef struct Particle Particle;
typedef std::vector<Particle> ParticleBuffer;
ParticleBuffer particles;
int main(){
GLuint nr_particles = 100;
for (GLuint i = 0; i < nr_particles; ++i) {
particles.push_back(Particle());
}
}
Solution
Using typedef had omitted the real problem, and that is in
typedef struct Y Particle;
Here Y wasn't ever defined, as I believed it was pulling the definition from the header (.hpp) file, but for some reason Microsoft Visual Studio (which i was using to compile & build) didn't link the header file, but gave no errors. Instead, it just behaved like Y doesn't exist and if we look at the definition of 'incomplete type':
Structure, union, or enumerations that have no definition
The error made sense now. The "workaround" solution was to use .h file instead of .hpp, so it would get linked. Also as @YSC pointed out in accepted answer, there is no need to use typedef, as the definition is correctly pulled from .h file it should give no errors.
Upvotes: 0
Views: 1013
Reputation: 40070
Your typedef
s are not needed, not wanted, and break things. Indeed, typedef struct Particle Particle;
is a C-ism. In C++, struct x {...};
introduces the name x
in the scope, no need to prefix it with struct
.
Keep it simple:
#include <vector>
struct Particle { /* ... */ }; // in real life, this is defined in a separate file
std::vector<Particle> GlobalVariablesAreEvil;
int main()
{
GlobalVariablesAreEvil.push_back(Particle{});
}
Upvotes: 8