【问题标题】:How to create an object inside another class with a constructor?如何使用构造函数在另一个类中创建对象?
【发布时间】:2012-08-04 20:41:37
【问题描述】:

所以我正在编写以模块化方式设计的代码。现在,我的一门课;名为Splash 必须创建另一个名为Emitter 的类的对象。通常你只需创建对象并完成它,但这在这里不起作用,因为Emitter 类有一个自定义构造函数。但是当我尝试创建一个对象时,它不起作用。

举个例子;

Emitter 有一个像这样的构造函数:Emitter::Emitter(int x, int y, int amount);,需要创建它以便可以在 Splash 类中访问。

我尝试这样做,但没有成功:

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

我也试过了,也没用:

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

编辑:我知道第二种方法应该有效,但它没有。如果我删除 Emitter 部分,代码就可以工作。但是当我使用第二种方法时,没有打开任何窗口,也没有执行任何应用程序。

那么如何创建我的Emitter 对象以用于Splash

编辑:

这是我的发射器类和标头代码: 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

这里是发射器函数:

#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();
    }
}

这里还有Splash ClassSplash Header

【问题讨论】:

  • 第二个版本是这样做的,它应该工作。你有错误吗?如果有,是什么样的?
  • 最后一种方法是如何失败的?看起来完全没问题。
  • 类Splash声明的结尾缺少分号?
  • 请发布一个完整的、最小的例子来说明问题。
  • 没有。它编译,但没有任何反应。 @Cheersandhth.-Alf 我会试着举个例子。

标签: c++ class object constructor


【解决方案1】:

第二个选项应该可以工作,我会开始查看编译错误,看看为什么它不工作。事实上,请发布与此代码相关的任何编译错误。

与此同时,您可以执行以下操作:

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; }

};

【讨论】:

  • 您能否详细说明为什么这会有所帮助?
  • @juanchopanza 它应该编译、工作并做他需要的事情。但是,我的猜测是它仍然不起作用,因为他的代码的问题不在于他发布的代码,而在于 Emitter 类中的其他内容。无论哪种方式,这都会揭示问题。
  • 嗯,我会发布发射器类。
  • 遵循使用指针的建议可能会引入额外的问题,而不是解决问题。特别是,给定的代码违反了三法则。即,如果 Splash 的任何实例被复制,它会做不圣洁的事情。
  • @Cheersandhth.-Alf 是的,我从未声称这是生产代码,并希望作者知道如何编写基本代码。如果它能让你开心,我可以添加一个复制构造函数和赋值运算符。 :)
【解决方案2】:

好吧,我设法以一种骇人听闻的方式修复了它。我所做的是创建一个默认构造函数,并将我的普通构造函数代码移动到一个新函数中。然后我创建了对象并调用了新的 init 函数来设置所有内容。

【讨论】:

  • 这表明您的代码中存在错误。我看到你有一个析构函数,但没有复制构造函数或赋值运算符,你在关注Rule of Three 吗?不要创建黑客,创建修复程序。
  • 我的启动类不需要复制构造函数,因为在程序开始时只生成 1,然后销毁。此外,splash 类的析构函数只是默认的。
  • 1) 如果不需要,删除它 (C++11) 或声明但不定义它 (C++03)。 2)好的,但除非你打算添加自定义行为,否则不要写出析构函数,让编译器来做。
  • 您也可以只将复制构造函数和赋值运算符设为私有并给它一个空定义。这样,任何使用该类的人在尝试进行复制或赋值时都会收到编译器错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多