C++ returning class object through multiple function - c++

I'm trying to pass custom object (TextureInfos) through multiples functions.
The call is:
TextureManager::Instance()->getTextureInfos("TEST",0)
TextureManager.cpp
TextureInfos& TextureManager::getTextureInfos(std::string key, int id)
{
TextureSet textureSet = textureSets[key];
return textureSet.getTextureInfos(id);
}
TextureSet .cpp
TextureInfos& TextureSet::getTextureInfos(int id)
{
sf::Texture texture;
sf::IntRect rect;
if (id < tileCount) {
int x = (id % maxCol) * tileWidth;
int y = (id / maxCol) * tileHeight;
rect.left = x;
rect.top = y;
rect.width = tileWidth;
rect.height = tileHeight;
}
TextureInfos *textureInfos = new TextureInfos(texture,rect);
return textureInfos;
}
I'm new to C++ and I think I miss something with the operator "&" and "*" etc. Because this code does not work for the moment...
Any help?
Thanks a lot.
EDIT:
Ok so the purpose of this code is to get a TextureInfos object at the end of the process. For this, I need to call the getTextureInfos method from TextureManager which also call getTextureInfos from TextureSet.
Here is the complete code for TextureManager.cpp
#include "TextureManager.h"
#include <iostream>
#include <fstream>
TextureManager TextureManager::m_TextureManager;
const std::string basePath="Assets/Graphics";
#pragma region Constructor
TextureManager::TextureManager()
{
textureSets.clear();
}
TextureManager::~TextureManager()
{
}
#pragma endregion
#pragma region Textures management
// Charge un set de texture a partir d'un nom de fichier
void TextureManager::LoadTextureset(std::string fileName,std::string key) {
TextureSet textureSet;
textureSet.init(basePath + fileName, key);
textureSets[key] = textureSet;
}
// Récupère une texture de la liste
TextureInfos TextureManager::getTextureInfos(std::string key, int id)
{
TextureSet textureSet = textureSets[key];
return textureSet.getTextureInfos(id); // HERE I GET AN ERROR
}
#pragma endregion
The line which is commented at the end is where i got an error:
no suitable user-defined conversion from "TextureInfos" to "TextureInfos" exists.
And for TextureSet.cpp:
#include "TextureSet.h"
#include <iostream>
#include <fstream>
#include "RapidXML\rapidxml.hpp"
#include "Debug.h"
const std::string basePath="Assets/Graphics";
using namespace rapidxml;
#pragma region Constructor
TextureSet::TextureSet()
{
}
TextureSet::~TextureSet()
{
}
#pragma endregion
void TextureSet::init(std::string l_filePath,std::string l_key)
{
filePath = l_filePath;
key = l_key;
// On détermine les URLs des fichiers
std::string setDescriptorPath = filePath + ".xml";
std::string setTilesetPath = filePath + ".png";
// On charge la texture
if (!textureSet.loadFromFile(setTilesetPath))
throw "ça load pas";
// On lis le xml
std::ifstream xmlDescriptor(setDescriptorPath);
if(!xmlDescriptor)
throw "Could not load tileset: " + setDescriptorPath;
std::string xmlDescriptorContents;
{
std::string line;
while(std::getline(xmlDescriptor, line))
xmlDescriptorContents += line;
}
std::vector<char> xmlData = std::vector<char>(xmlDescriptorContents.begin(), xmlDescriptorContents.end());
xmlData.push_back('\0');
//Create a parsed document with &xmlData[0] which is the char*
xml_document<> doc;
doc.parse<parse_no_data_nodes>(&xmlData[0]);
//Get the root node
xml_node<>* root = doc.first_node();
xml_node<>* imagefile = root->first_node("params");
maxRow = atoi(imagefile->first_attribute("maxRow")->value());
maxCol = atoi(imagefile->first_attribute("maxCol")->value());
tileWidth = atoi(imagefile->first_attribute("tileWidth")->value());
tileHeight = atoi(imagefile->first_attribute("tileHeight")->value());
tileCount = atoi(imagefile->first_attribute("tileCount")->value());
}
TextureInfos TextureSet::getTextureInfos(int id)
{
sf::Texture texture;
sf::IntRect rect;
if (id < tileCount) {
int x = (id % maxCol) * tileWidth;
int y = (id / maxCol) * tileHeight;
rect.left = x;
rect.top = y;
rect.width = tileWidth;
rect.height = tileHeight;
}
TextureInfos textureInfos(texture,rect);
return textureInfos;
}
TextureInfos.h
#include <unordered_map>
#include <vector>
#include <SFML\Graphics.hpp>
#include <string>
class TextureInfos
{
private:
protected:
public:
TextureInfos(sf::Texture& l_texture, sf::IntRect l_textureRect);
~TextureInfos();
sf::Texture& texture;
sf::IntRect textureRect;
};

No it doesn't work because it doesn't even compile. The reason it doesn't compile is because you try to return a pointer as a reference. A pointer and a reference are two different things.
For a quick, simple and dirty fix, change the return types from from TextureInfos& to TextureInfos*.
The quick fix outlined above is "dirty" because as you are using the code you will have a memory leak (you allocate memory with new but don't free it).
That can be solved two ways: Either return by value instead of using pointers/references. Or use smart pointers such as std::unique_ptr.

If you can avoid the use of dynamic memory allocation just edit your TextureSet::getTextureInfos to return a new stack object by value (notice that there will be a Return Value Optimization if you are worried about efficiency):
TextureInfos TextureSet::getTextureInfos(int id)
{
// ...
return TextureInfos(texture, rect);
}
Otherwise, if you really need dynamic allocation, use an std::shared_ptr to avoid memory leaks:
std::shared_ptr<TextureInfos> TextureSet::getTextureInfos(int id)
{
// ...
return std::make_shared<TextureInfos>(texture, rect);
}

According to your object naming I am assuming that you want to have a set of objects in your TextureSet. I would therefore suggest to add a private variable to your Object, and test new get requests against this object. You can then safely return a reference to a member.

One think that looks very worrying is that TextureInfos holds a reference to a texture, and you construct one from a passed reference.
TextureInfos(sf::Texture& l_texture, sf::IntRect l_textureRect);
sf::Texture& texture;
sf::IntRect textureRect;
Assuming the constructor does something like this
TextureInfos(sf::Texture& l_texture, sf::IntRect l_textureRect) :
texture(l_texture),
textureRect(l_textureRect)
{
}
... then you are entering a world of much pain. Look at GetTextureInfos() to see why:-
TextureInfos& TextureSet::getTextureInfos(int id)
{
sf::Texture texture;
sf::IntRect rect;
...
TextureInfos *textureInfos = new TextureInfos(texture,rect);
return textureInfos;
}
Ignoring the obvious issue that this will likely leak the returned object, you have the major problem that the returned object holds a reference to texture - but texture has gone out of scope so is no longer valid!
I can't propose a simple solution - but you need to make sure that you understand the lifetime management and ownership issues of the objects you're creating. Smart pointers (and returning entire objects rather than references) are usually key parts of this.

Related

Why is my class variable changing its value between methods?

I am trying to load a bitmap animation to the screen. I have a float variable holdTime that is specified to hold the "holdtime" value for the animation. In my constructor I set the holdtimevariable to 0.1f but when I try to access the method in the class that is using the holdTime variable, the value of holdTime has changed to -107374176f. So somewhere between my constructor call and the method call the value has changed from 0.1f to -107374176f.
To make things a little bit more clearer let me show you some code:
Here is the header file for the Game class, this is where I call the constructor of the Animation class that has the holdTime variable.
#pragma once
#include "Graphics.h"
#include "Surface.h"
#include "Animation.h"
#include "FrameTimer.h"
class Game
{
public:
Game( class MainWindow& wnd );
void Go();
private:
void UpdateModel();
private:
MainWindow& wnd;
FrameTimer ft;
Surface surf = Surface("Test32x48.bmp");
Animation testAnimation = Animation(0, 0, 32, 48, 4, surf, 0.1f);
};
You see that I have this testAnimation at the bottom of the class. The last argument in the constructor call is the value that is ought be in holdTime.
This is how my Animation header file looks like:
#include "Surface.h"
#include "Graphics.h"
#include <vector>
class Animation {
public:
Animation(int x, int y, int width, int height, int count, const Surface& sprite, float holdtime, Color chroma = Colors::Magenta);
void Update(float dt);
private:
void Advance();
private:
std::vector<RectI> frames;
int iCurFrame = 0;
float holdTime = 0;
float curFrameTime = 0.0f;
};
And this is the Animation Cpp file:
#include "Animation.h"
Animation::Animation(int x, int y, int width, int height, int count,
const Surface& sprite, float holdtime, Color chroma)
:
sprite(sprite),
holdTime(holdTime),
chroma(chroma)
{
for (int i = 0; i < count; i++)
{
frames.emplace_back(x + i * width, x + (i + 1) * width,y, y + height);
}
}
void Animation::Update(float dt)
{
curFrameTime += dt;
while(curFrameTime >= holdTime) {
Advance();
curFrameTime -= holdTime;
}
}
void Animation::Advance()
{
if (++iCurFrame >= frames.size()) {
iCurFrame = 0;
}
}
There is only one method that is making use of holdTime and that is the method Update(float dt).
If we go back to the Game class and look at the Game.cpp file:
#include "MainWindow.h"
#include "Game.h"
Game::Game( MainWindow& wnd )
:
wnd( wnd ),
gfx( wnd )
{
}
void Game::Go()
{
UpdateModel();
}
void Game::UpdateModel()
{
testAnimation.Update(ft.Mark());
}
In the Method Go() we call the method UpdateModel() which in turn is calling the Update() method in the animation class. This means that the first method to be executed in the Animation class after the constructor call is the update() method. When I debug the program I can see that the value of holdtime has changed between the constructor call and the Update() method call. But I don't know how since it I am not modifying the value somewhere else. It also seemes that the new value of holdTime is garbage value.
It became a lot of code in this question and it looks a bit messy and even though I lack the skills of writing a good Title I hope I made you somewhat clear what my problem is.
Thanks!
Update:
Here is the code for the FrameTimer class since the value returned from one of its methods is passed in into the Update() method:
FrameTimer.H:
#pragma once
#include <chrono>
class FrameTimer
{
public:
FrameTimer();
float Mark();
private:
std::chrono::steady_clock::time_point last;
};
FrameTimer.cpp:
#include "FrameTimer.h"
using namespace std::chrono;
FrameTimer::FrameTimer()
{
last = steady_clock::now();
}
float FrameTimer::Mark()
{
const auto old = last;
last = steady_clock::now();
const duration<float> frameTime = last - old;
return frameTime.count();
}
Edit:
main.cpp:
int WINAPI wWinMain( HINSTANCE hInst,HINSTANCE,LPWSTR pArgs,INT )
{
MainWindow wnd( hInst,pArgs );
Game game( wnd );
while( wnd.ProcessMessage() )
{
game.Go();
}
}
As you can see the game.Go() method is the first method that is called in main.
Your Animation constructor is at fault:
Animation::Animation(int x, int y, int width, int height, int count,
const Surface& sprite, float holdtime, Color chroma)
:
sprite(sprite),
holdTime(holdTime),
chroma(chroma)
{
for (int i = 0; i < count; i++)
{
frames.emplace_back(x + i * width, x + (i + 1) * width,y, y + height);
}
}
Here you attempt to initialise the member holdTime from the parameter holdTime.
Except, there is no parameter holdTime. There is only the parameter holdtime.
Hence instead you are actually initialising the member holdTime from itself (the next nearest "match" for that name), so it only retains its original, unspecified value (and in fact, reading an uninitialised variable results in your program having undefined behaviour).
So, you see, your member variable doesn't "change" at all — you never set it correctly. You'd have known that had you put some diagnostic output inside that constructor to examine the value and see whether it's what you thought it should be. None of the rest of the code was relevant or necessary.
A properly-configured compiler should have warned you about this.

using class ? c++ & sfml and 15 characters in the title

When using class ?
This question is probably evident for you but i know and learned how to create class and how it's working but i don't know when using it.
I think i can cut my program with class but i don't know when i must do it.
I would like to share my code but i can't paste it completely in the topic post.
Include
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
Constantes
//Constantes ecran
int tailleEcranX = 1280;
int tailleEcranY = 720;
//Constantes perso
int scalePerso = 3;
int tailleSpriteX = 32;
int tailleSpriteY = 48;
int speed(4);
int speedSprinte(20);
int milieuSpriteX = (tailleSpriteX/2)*scalePerso;
int milieuSpriteY = (tailleSpriteY/2)*scalePerso;
int pv = 100;
unsigned int pvMax = 100;
Initialisation
//Initiation des dessins
sf::RenderWindow window;
sf::RectangleShape rect;
sf::Texture perso;
sf::Sprite sprite_perso;
sf::View view;
sf::RectangleShape rectCol;
sf::RectangleShape pvBar;
sf::RectangleShape pvMaxBar;
enum Dir{Down,Left,Right,Up};
sf::Vector2i anim (1,Down);
#include "gestion_clavier.h"
Main
int main()
{
//{ Positionnement des objets
window.create(sf::VideoMode(tailleEcranX , tailleEcranY), "The Game I");
window.setPosition(sf::Vector2i(500,250));
window.setFramerateLimit(60);
rect.setFillColor(sf::Color(255,0,0));
rect.setSize(sf::Vector2f(tailleEcranX-10,tailleEcranY-10));
rect.setPosition(5,5);
rect.setOutlineColor(sf::Color(255,255,255));
rect.setOutlineThickness(3);
rectCol.setFillColor(sf::Color(0,0,200));
rectCol.setSize(sf::Vector2f(50,50));
rectCol.setPosition(400,500);
rectCol.setOutlineColor(sf::Color(255,255,255));
rectCol.setOutlineThickness(1);
sf::Clock time;
//}
//{Chargement des Sprites
if (!perso.loadFromFile("link/srpite.png",sf::IntRect(0,0,96,192)))
{
std::cout<<"erreur chargement player image"<<std::endl;
}
sprite_perso.setTexture(perso);
sprite_perso.setPosition(tailleEcranX/2-milieuSpriteX,tailleEcranY/2-milieuSpriteY);
//}
//{ Game Loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
ProcessInput();
//gestion_clavier();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)||sf::Keyboard::isKeyPressed(sf::Keyboard::S)||sf::Keyboard::isKeyPressed(sf::Keyboard::D)||sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
if (time.getElapsedTime().asMilliseconds()>= 50)
{
anim.x++;
if(anim.x*tailleSpriteX >= perso.getSize().x)
anim.x=0;
time.restart();
}
}
sprite_perso.setTextureRect(sf::IntRect(anim.x*tailleSpriteX,anim.y*tailleSpriteY,tailleSpriteX,tailleSpriteY));
sprite_perso.setScale(scalePerso,scalePerso);
pvBar.setFillColor(sf::Color(20,255,30));
pvBar.setSize(sf::Vector2f(4*pv,10));
pvBar.setPosition(20,20);
pvMaxBar.setFillColor(sf::Color(0,0,0));
pvMaxBar.setSize(sf::Vector2f(4*pvMax,10));
pvMaxBar.setPosition(20,20);
pvMaxBar.setOutlineColor(sf::Color(255,255,255));
pvMaxBar.setOutlineThickness(2);
if(pv>=pvMax)
{
pv=pvMax;
}
if(pv<=0)
{
pv=0;
}
if(
(std::abs((sprite_perso.getPosition().x+milieuSpriteX)-(rectCol.getPosition().x+50/2))<50)
&&
(std::abs((sprite_perso.getPosition().y+milieuSpriteY)-(rectCol.getPosition().y+50/2))<50)
)
{
std::cout<<"collision"<<std::endl;
pv--;
std::cout<<pv<<std::endl;
}
//Dessinage
window.draw(rect);
window.draw(rectCol);
window.draw(sprite_perso);
window.draw(pvMaxBar);
window.draw(pvBar);
window.display();
window.clear();
}
//}
return 0;
}
//}
I struggled to understand a lot of your code, since the variables seem to be written in french, but I can give you some general advice.
When deciding when to use a class, you should first think about the contents of your program in terms of objects. For example: if there is a character/person, or something you would consider an independent object in the real world, there's a reasonable chance that should be its own class. The class should contain whatever is needed to describe the object, like it's position, scale, sprite, etc. Here's an example:
class Person
{
private:
sf::Vector2f position;
sf::Sprite sprite;
public:
void setPosition(sf::Vector2f newPosition);
sf::Vector2f getPosition();
void draw(sf::RenderWindow& window);
}
If you want your class to have position and such, I recommend deriving from the sf::Transformable class to save you some time and effort. You can also derive from sf::Drawable if you want to draw your object using window.draw(person) like many of the default SFML objects can.
class Person : public sf::Transformable, public sf::Drawable
{
private:
sf::Sprite sprite;
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(sprite, states);
}
}
You have a number of variables you say are constants. If that is the case, then it's considered good practice to mark them as such using const. For example:
const int scalePerso = 3;
This stops you from changing the value in the future and causing bugs you need to track down.
I also noticed you are defining seperate variables for X and Y positions. This is unnecessary, since SFML defines the sf::Vector2 type. For example:
const sf::Vector2f tailleSpritePosition(32.f, 48.f);
instead of
int tailleSpriteX = 32;
int tailleSpriteY = 48;
You're also mixing types quite a bit by passing integers into functions that accept float values. This is also considered bad practice. If you want to change an integer into a float, you should explicitly cast it using (float) like so:
rect.setSize(sf::Vector2f((float)tailleEcranX - 10.f, (float)tailleEcranY - 10.f));
making sure to add the letter f to the end of your literals to tell the compiler those are floating point values too. Alternatively, you could just define your variables to be of type float to start off with in some cases.
Feel free to ask if there is anything you don't understand.

Access violation for object

I have an Entity.h like this:
using namespace physx;
class Entity
{
public:
Entity(Ogre::Vector3 dims, Ogre::Vector3 pos, std::string mesh, std::string id);
virtual ~Entity(void);
virtual void update(Ogre::Real dt);
virtual void init(Ogre::SceneManager* sceneMgr, PxPhysics* physics, PxScene* scene, PxVec3 velocity=PxVec3(0, 0, 0));
protected:
Ogre::Entity* mOgreEntity = NULL;
Ogre::SceneNode* mOgreNode = NULL;
Ogre::Vector3 mPosition;
Ogre::Vector3 mDimensions;
std::string mMesh;
std::string mId;
PxRigidDynamic* mActor;
PxMaterial* mMaterial;
};
And here is my Entity source:
#include "Entity.h"
Entity::Entity(Ogre::Vector3 dims, Ogre::Vector3 pos, std::string mesh, std::string id)
{
mDimensions = dims;
mPosition = pos;
mMesh = mesh;
mId = id;
mActor = NULL;
mMaterial = NULL;
}
Entity::~Entity(void)
{
}
void Entity::update(Ogre::Real dt)
{
PxVec3 pos = mActor->getGlobalPose().p;
Ogre::Real r = 0;
mOgreNode->setPosition(Ogre::Vector3(pos.x + r, pos.y + r, pos.z + r));
}
void Entity::init(Ogre::SceneManager* sceneMgr, PxPhysics* physics, PxScene* scene, PxVec3 velocity)
{
// Create an Entity
mOgreEntity = sceneMgr->createEntity(mId, mMesh);
mOgreEntity->setCastShadows(true);
// Create a SceneNode and attach the Entity to it
mOgreNode = sceneMgr->getRootSceneNode()->createChildSceneNode(mId + "Node");
Ogre::AxisAlignedBox box = mOgreEntity->getBoundingBox();
Ogre::Vector3 realSizes = box.getSize();
mOgreNode->setPosition(mPosition);
mOgreNode->attachObject(mOgreEntity);
Ogre::Vector3 scaler = Ogre::Vector3(mDimensions.x / realSizes.x, mDimensions.y / realSizes.y, mDimensions.z / realSizes.z);
mOgreNode->scale(scaler);
mMaterial = physics->createMaterial(1.5f, 1.5f, 1.0f);
PxGeometry* geometry = NULL;
if(mMesh == "sphere.mesh")
{
PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
geometry = &g;
} else {
// geometry = NULL;
}
PxTransform transform = PxTransform(PxVec3(mPosition.x, mPosition.y, mPosition.z));
mActor = PxCreateDynamic(*physics, transform, *geometry, *mMaterial, PxReal(.1));
// if(!mActor) {
// MessageBox( NULL, "no actor", "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
// return;
// }
mActor->setLinearVelocity(velocity);
// And add the actor to a scene:
scene->addActor(*mActor);
}
Now, if I create a single entity and initialize it works. Even wirh a second entity var it works as well. Now with an array:
Entity *mEntities[20];
for(int i = 0 ; i < 20 ; i++ ){
ostringstream nameStream;
nameStream << "Sphere_" << i;
string name = nameStream.str();
Entity* sphere = new Entity(Ogre::Vector3(i*5, i*4.5, i*6), Ogre::Vector3(i*5, i*4.5, i*6), "sphere.mesh", name);
sphere->init(mSceneMgr, mPhysics, gScene, PxVec3(-10.0f, 0, 0));
mEntities[i] = sphere;
}
I got Access violation. W/ the just-in-time debugger, it turned out that mActorwas null as well as mMaterial
EDIT:
This code does not work either:
mEntity = Entity(Ogre::Vector3(50.0f, 50.0f, 50.0f), Ogre::Vector3(50.0f, 40.5f, 60.0f), "sphere.mesh", "sphere");
mEntity.init(mSceneMgr, mPhysics, gScene, PxVec3(-10.0f, 0, 0));
1)
Entity* sphere = new Entity(Ogre::Vector3(i*5, i*4.5, i*6),
Ogre::Vector3(i*5, i*4.5, i*6),
"sphere.mesh",
"Sphere_"+i);
Look at the "Sphere_"+i
If the i is larger then length of ”Sphere_” you are passing pointer to some random memory. I assume that you wanted to create a string with i at the end.
Use sprintf or std::string for that.
2)
If you change the loop range from 20 to let's say 3 it will probably work. The problem is that your names will be:
Sphere_, phere_, here_
Because by doing "Sphere_"+i you are not adding integer to the string.
This is "equal" to:
char *string = "String";
string += 3;
3)
This code will generate string that you need:
std::ostringstream newStringStream;
newStringStream << "Sphere_" << i;
std::string newString = newStringStream.str();
Here is another issue:
PxGeometry* geometry = NULL;
if(mMesh == "sphere.mesh")
{
geometry = &PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
}
The problem with this is that you are assigning to geometry the address of a temporary value. Once that line of code is completed, that temporary is gone.
The probable fix for this is to do this:
PxGeometry geometry;
if(mMesh == "sphere.mesh")
{
geometry = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
}
//...
mActor = PxCreateDynamic(*physics, transform, geometry, *mMaterial, PxReal(.1));
Now geometry is no longer a pointer, and you're assigning geometry to the value returned, (not address-of the value returned).
I am reading the documentation here:
http://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/apireference/files/classPxSphereGeometry.html
So PxSphereGeometry(x) is a constructor call. So you need to assign the return value to a PxShpereGeometry, not a PxSphereGeometry*.
Edit: Your latest changes also do not have the desired effect:
if(mMesh == "sphere.mesh")
{
PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
geometry = &g;
}
The g is local to the if() block. You assign the address of this g to geometry. Then when that block exits, g is gone, and now you have geometry pointing to something that no longer exists.
The difference between your edited code and the answer I gave is that my answer assigns the return value to an existing object. So I created a duplicate of the return value. What your doing in the edited code is not creating a duplicate, but pointing to a local object, which as explained, won't exist after it leaves scope.
So if you were to write code that follows the pattern of your edited code, and have it be valid, the change would look like this:
PxGeometry geometry;
if(mMesh == "sphere.mesh")
{
PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
geometry = g;
}
However, this does extraneous work. The original answer is sufficient.
I tried the alternate way to create a rigid body and it worked!!!!
mActor = physics->createRigidDynamic(PxTransform(PxVec3(mPosition.x, mPosition.y, mPosition.z)));
PxShape* shape = mActor->createShape(PxSphereGeometry(mDimensions.x / 2), *mMaterial);
PxRigidBodyExt::updateMassAndInertia(*mActor, 0.4f);

Can't pass a string to my class function

I have a class with this function:
void Render(SDL_Surface *source,SDL_Surface *destination,string img)
{
SDL_Rect offset;
offset.x = m_x;
offset.y = m_y;
source = IMG_Load(img);
offset.w = source->w;
offset.h = source->h;
}
For some reason though even with include <string> at the top of the header file it won't allow it. I get:
Identifier, "string" is undefined.
Im passing the data like this on my main file:
btn_quit.Render(menu,screen,"button.png");
When i execute i get :
'Button::Render' : function does not take 3 arguments
But this site says string is the correct syntax for the data type (at the bottom): http://www.cplusplus.com/doc/tutorial/variables/
Can some one explain what I am doing wrong ?
I may suggest you change Render function to below:
void Render(SDL_Surface *source,SDL_Surface *destination,const std::string& img)
{
SDL_Rect offset;
offset.x = m_x;
offset.y = m_y;
source = IMG_Load(img.c_str());
offset.w = source->w;
offset.h = source->h;
}
use std::string instead of string
pass img reference instead of passing by value
change from IMG_Load(img); to IMG_Load(img.c_str());

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. 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. 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); and needs to be created so it can be accessed in the Splash class.
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. 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?
Edit:
Here is my code for the emitter class and 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.
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.