简体   繁体   English

如何使用构造函数在另一个类中创建对象?

[英]How to create an object inside another class with a constructor?

So I was working on my code, which is designed in a modular way. 因此,我正在研究以模块化方式设计的代码。 Now, one of my classes; 现在,我的一堂课; called Splash has to create a object of another class which is called Emitter . 名为Splash必须创建另一个类的对象,称为Emitter Normally you would just create the object and be done with it, but that doesn't work here, as the Emitter class has a custom constructor. 通常,您只需要创建对象并完成操作即可,但这在这里不起作用,因为Emitter类具有自定义构造函数。 But when I try to create an object, it doesn't work. 但是当我尝试创建对象时,它不起作用。

As an example; 举个例子;

Emitter has a constructor like so: Emitter::Emitter(int x, int y, int amount); Emitter具有如下构造函数: Emitter::Emitter(int x, int y, int amount); and needs to be created so it can be accessed in the Splash class. 并且需要创建,以便可以在Splash类中对其进行访问。

I tried to do this, but it didn't 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 the second way is supposed to work, however it doesn't. 编辑:我知道第二种方法应该工作,但是没有。 If I remove the Emitter Section, the code works. 如果删除“ Emitter部分,则代码有效。 but when I do it the second way, no window opens, no application is executed. 但是当我以第二种方式进行操作时,没有打开窗口,没有执行任何应用程序。

So how can I create my Emitter object for use in Splash ? 那么,如何创建在Splash使用的Emitter对象?

Edit: 编辑:

Here is my code for the emitter class and header: 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 is 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 is the Splash Class and the Splash Header . 这里也是Splash类Splash标头

The second option should work, and I would start looking at compilation errors to see why it doesn't. 第二个选项应该起作用,我将开始查看编译错误,以了解为什么不行。 In fact, please post any compilation errors you have related to 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; }

};

Well, I managed to fix it, in a hackish way though. 好吧,尽管如此,我还是设法解决了。 What I did was create a default constructor, and move my normal Constructor code into a new function. 我要做的是创建一个默认的构造函数,然后将我的常规“构造函数”代码移到新函数中。 Then I created the object and called the the new init function to set everything up. 然后,我创建了该对象,并调用了新的init函数来设置所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在类的构造函数中创建对象是否有效? - Is it valid to create an object inside the constructor of class? 如何在另一个类中调用一个类的构造函数? - How to call a constructor for a class inside another class? 如何使用构造函数创建对象,该构造函数在另一个类的构造函数中获取堆栈上的参数 - How to create an object using a constructor that takes arguments on stack in constructor of another Class 如何在另一个类中调用自定义构造函数 - How to call a custom constructor inside another class 如何在C++中的另一个类中声明一个类的构造函数 - How to declare a constructor of a class inside another class in C++ 如何在另一个类头文件中定义类构造函数? - How to define class constructor inside another class header file? 如何在另一个类中创建一个类的参数化构造函数作为数据成员? - How to create parameterized constructor of a class in another class as a data member? 如何在没有默认构造函数的其他类中创建模板类 - how to create template class in another class with out default constructor 如何在一个类的构造函数中实例化到另一个类的对象? C ++ - How to instantiate an object to another class in the constructor of a class? C++ 如何使用另一个类的对象为Class编写复制构造函数 - How to write copy constructor for Class with object of another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM