How to create an object inside another class using a constructor?

So, I worked on my code, which was developed in a modular way. Now one of my classes; called Splash should create an object of another class called Emitter . Usually you just create an object and execute it, but that does not work here, since the Emitter class has its own constructor. But when I try to create an object, it does not work.

As an example:

Emitter has the following constructor: Emitter::Emitter(int x, int y, int amount); and it needs to be created so that it can be obtained in the Splash class.

I tried to do this, but this did not work:

 class Splash{ private: Emitter ps(100, 200, 400, "firstimage.png", "secondimage.png"); // Try to create object, doesn't work. public: // Other splash class functions. } 

I also tried this, which didn't work either:

 class Splash{ private: Emitter ps; // Try to create object, doesn't work. public: Splash() : ps(100, 200, 400, "firstimage.png", "secondimage.png") {}; } 

Edit: I know that the second way should work, but it is not. If I delete the Emitter section, the code will work. but when I do it in the second way, no window opens, the application is not running.

So how can I create an Emitter object for use in Splash ?

Edit:

Here is my code for the class and emitter header: Header

 // Particle engine for the project #ifndef _PARTICLE_H_ #define _PARTICLE_H_ #include <vector> #include <string> #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "image.h" extern SDL_Surface* gameScreen; class Particle{ private: // Particle settings int x, y; int lifetime; private: // Particle surface that shall be applied SDL_Surface* particleScreen; public: // Constructor and destructor Particle(int xA, int yA, string particleSprite); ~Particle(){}; public: // Various functions void show(); bool isDead(); }; class Emitter{ private: // Emitter settings int x, y; int xVel, yVel; private: // The particles for a dot vector<Particle> particles; SDL_Surface* emitterScreen; string particleImg; public: // Constructor and destructor Emitter(int amount, int x, int y, string particleImage, string emitterImage); ~Emitter(); public: // Helper functions void move(); void show(); void showParticles(); }; #endif 

and here are the emitter functions:

 #include "particle.h" // The particle class stuff Particle::Particle(int xA, int yA, string particleSprite){ // Draw the particle in a random location about the emitter within 25 pixels x = xA - 5 + (rand() % 25); y = yA - 5 + (rand() % 25); lifetime = rand() % 6; particleScreen = Image::loadImage(particleSprite); } void Particle::show(){ // Apply surface and age particle Image::applySurface(x, y, particleScreen, gameScreen); ++lifetime; } bool Particle::isDead(){ if(lifetime > 11) return true; return false; } // The emitter class stuff Emitter::Emitter(int amount, int x, int y, string particleImage, string emitterImage){ // Seed the time for random emitter srand(SDL_GetTicks()); // Set up the variables and create the particles x = y = xVel = yVel = 0; particles.resize(amount, Particle(x, y, particleImage)); emitterScreen = Image::loadImage(emitterImage); particleImg = particleImage; } Emitter::~Emitter(){ particles.clear(); } void Emitter::move(){ } void Emitter::show(){ // Show the dot image. Image::applySurface(x, y, emitterScreen, gameScreen); } void Emitter::showParticles(){ // Go through all the particles for(vector<Particle>::size_type i = 0; i != particles.size(); i++){ if(particles[i].isDead() == true){ particles.erase(particles.begin() + i); particles.insert(particles.begin() + i, Particle(x, y, particleImg)); } } // And show all the particles for(vector<Particle>::size_type i = 0; i != particles.size(); i++){ particles[i].show(); } } 

Also here are Splash Class and Splash Header .

+4
source share
2 answers

The second option should work, and I would start looking at compilation errors to understand why this is not so. In fact, submit any compilation errors associated with this code.

In the meantime, you can do something like this:

 class Splash{ private: Emitter* ps; public: Splash() { ps = new Emitter(100,200,400); } Splash(const Splash& copy_from_me) { //you are now responsible for this } Splash & operator= (const Splash & other) { //you are now responsible for this} ~Splash() { delete ps; } }; 
+8
source

Well, I managed to fix it, although I hack it. I created a default constructor and translated my regular constructor code into a new function. Then I created an object and called a new init function to set everything up.

0
source

All Articles