如何使用构造函数在另一个类中创建对象
How to create an object inside another class with a constructor?
所以我正在研究我的代码,它是以模块化方式设计的。现在,我的一堂课;调用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
部分,代码就可以工作。但是当我以第二种方式执行此操作时,没有打开窗口,没有执行任何应用程序。
那么如何创建用于Splash
的Emitter
对象呢?
编辑:
这是我的发射器类和标头的代码: 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 类和 Splash 标头。
第二个选项应该有效,我会开始查看编译错误以了解为什么它不起作用。实际上,请发布与此代码相关的任何编译错误。
同时,您可以执行以下操作:
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; }
};
好吧,我设法以一种黑客方式修复了它。我所做的是创建一个默认构造函数,并将我的普通构造函数代码移动到一个新函数中。然后我创建了对象并调用了新的 init 函数来设置所有内容。
相关文章:
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何创建一个共享对象与另一个.所以在Cmake
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 从主函数中删除在另一个函数中使用 new 创建的一系列对象
- 在另一个对象 B 中创建对象 A 时,对象 A 是否是对象 B 的本地对象,对象 A 是否会存在于对象 B 的实例化之外?
- 无法将类对象创建为另一个类的成员
- 在类中创建指针到另一个对象
- 创建在另一个项目中定义的类对象
- 在矢量内创建一个对象,该对象由另一个对象包含
- 将向量从另一个对象添加到创建对象的对象
- 使用另一个(现有)对象创建新对象时会发生什么情况?
- 在一个线程中创建一个对象,并使用std :: Atomic访问另一个对象
- 用另一个对象作为构造函数中的参数创建对象
- 如何在另一个对象的构造函数中声明和创建对象?
- 如何使用在另一个类的构造函数中的堆栈上接受参数的构造函数创建对象
- 如何创建从另一个派生的两个ATL接口
- 创建新对象似乎会更改之前创建的另一个对象 (???)
- 删除对象(另一个..)时双重释放或损坏
- 创建指向另一个类中对象的链表时遇到问题