Beginner problems making my own class

hi,

i try to make my own class and functions but i don’t know how to initialise scene, cache or others nodes within it so that when i use my function in the main.cpp, it use the nodes already set.
could someone tell me how to do that please ?
here is the function i’m trying to make.

void Module::addRandomModule(Scene* scene, ResourceCache* cache, int m_id)
{

    const char* moduleX;

    //XML
    tinyxml2::XMLDocument modules;
    modules.Parse("Data/Scripts/Modules.xml");
    if(modules.LoadFile("Data/Scripts/Modules.xml") == tinyxml2::XML_NO_ERROR)
    {
        tinyxml2::XMLNode* root = modules.FirstChild();
        tinyxml2::XMLElement* infos = root->FirstChildElement("infos");
        infos->QueryIntAttribute("count", &modulesCount);
        int randomLine = chooseRandomLine(modulesCount);

        std::string module = ("module");
        {
            std::ostringstream ss;
            ss<<randomLine;
            std::string s(ss.str());
            module.append(s);
        }
        moduleX = module.c_str();

        tinyxml2::XMLElement* randomModule = root->FirstChildElement(moduleX);
        randomModule->QueryIntAttribute("type", &m_type); //moduletype);
        randomModule->QueryIntAttribute("exits", &m_exits);
           
        m_path = randomModule->Attribute("path");
        m_texturepath = randomModule->Attribute("texturepath");
    }

    std::string moduleName = ("");
    {
        std::ostringstream ss;
        ss<<m_id<<moduleX;
        std::string s(ss.str());
        moduleName.append(s);
    }
    m_name = moduleName.c_str();


    moduleNode=scene->CreateChild(m_name); 
    moduleNode->SetPosition(Vector3(0,0,0));
    AnimatedModel* moduleObject=moduleNode->CreateComponent<AnimatedModel>();
    moduleObject->SetModel(cache->GetResource<Model>(m_path));
    moduleObject->SetMaterial(cache->GetResource<Material>(m_texturepath));

}

the class WIP :

[spoiler]module.h

[code]
#ifndef MODULE_H
#define MODULE_H

#include <Urho3D/Math/Vector3.h>
using namespace Urho3D;

class Module
{
private:
int m_id;
const char* m_name;
int m_type;
int m_exits;
const char* m_path;
const char* m_texturepath;

public:
Module(); //constructeur
~Module(); //destructeur

void addRandomModule(Scene* scene, ResourceCache* cache, int m_id);
int getType(void);
int getExits(void);

void setPosition(Vector3 modulePos);

};

#endif[/code]

module.cpp

[code]
#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Input/InputEvents.h>

#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Button.h>
#include <Urho3D/UI/UIEvents.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Geometry.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/DebugRenderer.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/AnimatedModel.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Graphics/Skybox.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Math/Vector3.h>

#include
#include
#include
#include
#include <time.h>

//my stuff
#include “tinyxml2.h”
#include “fonctions.h”
#include “Module.h”

int modulesCount;

Module::Module() //constructeur qui initialise
{
int m_id; // = 0;
const char* m_name;
int m_type; // = 0;
int m_exits; // = 0;
const char* m_path; // = “”;
const char* m_texturepath; // = “”;
}

Module::~Module() //destructeur
{

}

void Module::addRandomModule(Scene* scene, ResourceCache* cache, int m_id)
{

const char* moduleX;

//XML
tinyxml2::XMLDocument modules;
modules.Parse("Data/Scripts/Modules.xml");
if(modules.LoadFile("Data/Scripts/Modules.xml") == tinyxml2::XML_NO_ERROR)
{
    tinyxml2::XMLNode* root = modules.FirstChild();
    tinyxml2::XMLElement* infos = root->FirstChildElement("infos");
    infos->QueryIntAttribute("count", &modulesCount);
    int randomLine = chooseRandomLine(modulesCount);

    std::string module = ("module");
    {
        std::ostringstream ss;
        ss<<randomLine;
        std::string s(ss.str());
        module.append(s);
    }
    moduleX = module.c_str();

    tinyxml2::XMLElement* randomModule = root->FirstChildElement(moduleX);
    randomModule->QueryIntAttribute("type", &m_type); //moduletype);
    randomModule->QueryIntAttribute("exits", &m_exits);
       
    m_path = randomModule->Attribute("path");
    m_texturepath = randomModule->Attribute("texturepath");
}

std::string moduleName = ("");
{
    std::ostringstream ss;
    ss<<m_id<<moduleX;
    std::string s(ss.str());
    moduleName.append(s);
}
m_name = moduleName.c_str();

moduleNode=scene->CreateChild(m_name); 
moduleNode->SetPosition(Vector3(0,0,0));
AnimatedModel* moduleObject=moduleNode->CreateComponent<AnimatedModel>();
moduleObject->SetModel(cache->GetResource<Model>(m_path));
moduleObject->SetMaterial(cache->GetResource<Material>(m_texturepath));

}

int Module::getType(void)
{
return m_type;
}

int Module::getExits(void)
{
return m_exits;
}

void Module::setPosition(Vector3 modulePos)
{

}[/code][/spoiler]

i’m sorry, i didn’t mean to troll or something, the code for that is kinda ok in my previous post. it just missed the initialisation of the moduleNode but i have some crash problems, it drives me a little crazy.
problems with initialisation to null and stuff like that, i never had to use it before and surprisingly, all my stuff worked fine before so i have a little hard time initialising everything fine. ><

Initializing member values in the constructor head is preferable over redefining them in the constructor body:

Module::Module() : //constructeur qui initialise m_id{0}, m_name{}, m_type{0}, m_exits{0}, m_path{""}, m_texturepath{""} { }

Rather than creating an empty destructor, don’t write one at all. (From Stroustrup’s Tour of C++)

thx but what is the difference ? is the access method still the same ?

here i’m struggling trying to use GetWorldPosition() in my own getPosition() but i didn’t even defined a position to begin with in my class… >< i hate classes.

The way you had set up the statements in the constructor, with data types mentioned, they were being redefined. This means a temporary object with the same name is created that is deleted at the end of the constructor’s body, causing ambiguity. This is obviously not what you want in this case.
Just leaving out the data types and removing the slashes would have been enough, but initializing them this way is less error prone. Why?

thx for the link. i have the book actually (i guess i will check the whole chapter later) but i never really paid attention to memory allocation.

about the temporary object, you’re right, the modules are to build a dungeon, i won’t change them while running of later for fun but that’s not my priority.
if i can do my class properly, next i already need to do another one that will represent each exits for each random modules, and then i will need to stock them and be able to update their list or whatever. ^^;

[quote=“Modanung”]Initializing member values in the constructor head is preferable over redefining them in the constructor body:

Module::Module() : //constructeur qui initialise m_id{0}, m_name{}; m_type{0}, m_exits{0}, m_path{""}, m_texturepath{""} } }

Rather than creating an empty destructor, don’t write one at all. (From Stroustrup’s Tour of C++)[/quote]

it seem i can’t use your method, it don’t compile because it don’t use c++11.
and the other method compile but my application crash and i don’t understand the message of the debugger. it seem i’m missing a file but i wonder why it would need it all of a sudden lol

[quote]Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6990742 in _IO_vfscanf_internal (s=s@entry=0x7fffffffd460,
format=format@entry=0xe7b39c “%d”, argptr=argptr@entry=0x7fffffffd588,
errp=errp@entry=0x0) at vfscanf.c:1857
1857 vfscanf.c: Aucun fichier ou dossier de ce type.
[/quote]

could you help me correct my class please ? or do you know how i can set my compiler to not have this problem ? i installed the build-essential packet on ubuntu and use make to compile, i think i need to add a -std=c++11 or -std=gnu++11 option somewhere but i don’t know where.

module.h

#ifndef MODULE_H
#define MODULE_H

#include <Urho3D/Math/Vector3.h>
using namespace Urho3D;

class Module
{
    private:
    Node* m_node;
    int m_id;
    const char* m_name;
    int m_type;
    int m_exits;
    const char* m_path;
    const char* m_texturepath;
    Vector3 m_position;

    public:
    Module(); //constructeur

    void addRandomModule(Scene* scene, ResourceCache* cache, int m_id);
    int getType(void);
    int getExits(void);

    Vector3 getPosition();
    void setPosition(Vector3 position);
};

#endif

module.cpp

#include <Urho3D/Engine/Application.h>
#include <Urho3D/Engine/Engine.h>
#include <Urho3D/Input/InputEvents.h>

#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/Resource/ResourceCache.h>
#include <Urho3D/Resource/XMLFile.h>
#include <Urho3D/IO/Log.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Button.h>
#include <Urho3D/UI/UIEvents.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Scene/Scene.h>
#include <Urho3D/Scene/SceneEvents.h>
#include <Urho3D/Graphics/Graphics.h>
#include <Urho3D/Graphics/Camera.h>
#include <Urho3D/Graphics/Geometry.h>
#include <Urho3D/Graphics/Renderer.h>
#include <Urho3D/Graphics/DebugRenderer.h>
#include <Urho3D/Graphics/Octree.h>
#include <Urho3D/Graphics/Light.h>
#include <Urho3D/Graphics/Model.h>
#include <Urho3D/Graphics/StaticModel.h>
#include <Urho3D/Graphics/AnimatedModel.h>
#include <Urho3D/Graphics/Material.h>
#include <Urho3D/Graphics/Skybox.h>
#include <Urho3D/UI/Window.h>
#include <Urho3D/Math/Vector3.h>


#include <string>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <time.h>



//my stuff
#include "tinyxml2.h"
#include "fonctions.h"
#include "Module.h"

int modulesCount; //setting file class ?

Module::Module()//constructeur qui initialise,  cant use list without c++11
{
    Node* m_node;
    int m_id=0;
    const char* m_name="";
    int m_type=0;
    int m_exits=0;
    const char* m_path="";
    const char* m_texturepath="";
    Vector3 m_position(0, 0, 0);
}

void Module::addRandomModule(Scene* scene, ResourceCache* cache, int m_id)
{

    const char* moduleX;

    //XML
    tinyxml2::XMLDocument modules;
    modules.Parse("Data/Scripts/Modules.xml");
    if(modules.LoadFile("Data/Scripts/Modules.xml") == tinyxml2::XML_NO_ERROR)
    {
        tinyxml2::XMLNode* root = modules.FirstChild();
        tinyxml2::XMLElement* infos = root->FirstChildElement("infos");
        infos->QueryIntAttribute("count", &modulesCount);
        int randomLine = chooseRandomLine(modulesCount);

        std::string module = ("module");
        {
            std::ostringstream ss;
            ss<<randomLine;
            std::string s(ss.str());
            module.append(s);
        }
        moduleX = module.c_str();

        tinyxml2::XMLElement* randomModule = root->FirstChildElement(moduleX);
        randomModule->QueryIntAttribute("type", &m_type); //moduletype);
        randomModule->QueryIntAttribute("exits", &m_exits);
           
        m_path = randomModule->Attribute("path");
        m_texturepath = randomModule->Attribute("texturepath");
    }

    std::string moduleName = ("");
    {
        std::ostringstream ss;
        ss<<m_id<<moduleX;
        std::string s(ss.str());
        moduleName.append(s);
    }
    m_name = moduleName.c_str();


    m_node=scene->CreateChild(m_name);
    m_node->SetPosition(m_position);
    AnimatedModel* moduleObject=m_node->CreateComponent<AnimatedModel>();
    moduleObject->SetModel(cache->GetResource<Model>(m_path));
    moduleObject->SetMaterial(cache->GetResource<Material>(m_texturepath));

}

int Module::getType(void)
{
    return m_type;
}

int Module::getExits(void)
{
    return m_exits;
}

void Module::setPosition(Vector3 position)
{
    m_node->SetPosition(position);
}

Vector3 Module::getPosition(void)
{
    return m_node->GetWorldPosition();
}

and i tryed to use it like that in the main.cpp

Module* module1;
module1->addRandomModule(my_scene, cache, 1);
module1->setPosition(Vector3(0,0,0));

nevermind, i will try using struct and reorganising my code. i kinda want to do everything at the same time but i lack methodology here.

The other problem with that is faster to initialize a variable rather than initialize it and then assign a value to it inside the constructor.Constructor allows you to initilize member variables by a specific value in one go (e.g it is lot faster).

[quote]it seem i can’t use your method, it don’t compile because it don’t use c++11.
and the other method compile but my application crash and i don’t understand the message of the debugger. it seem i’m missing a file but i wonder why it would need it all of a sudden lol[/quote]

Thats nothing to do with c++ 11. class member initialization was always part of c++. The problem is Modanung made a mistake by using curly brackets after the variables and not round brackets.
This should work :

Module::Module() : //constructeur qui initialise m_id(0), m_name(), m_type(0), m_exits(0), m_path(0), m_texturepath(0) } {

Also is there a reason you use const char* for strings ? Why not just use Urho’s String ? You could also create classes derived from LogicComponent so you can easily access the scene or the component’s node or all the SubSystems inside the component

C++ Core Guidelines

C++ Core Guidelines[/quote]

You can’t initialize variables with {} in a constructor

This is OK

SomeClass::SomeClass(): member1(0), member2(123), stringMemebr("something") { }

This is worong :

SomeClass::SomeClass(): member1{0}, member2{123}, stringMemebr{"something"} { }

Try it

Tried it , it doesn’t work…

Update something

Update something[/quote]

Thats nothing to do with updating… it just doesn’t work.I used hundreds of open source c++ libraries , and not a single one used {} for member initializing.
From the other hand you first post doesn’t even make sense :

[quote]Module::Module() : //constructeur qui initialise
m_id{0},
m_name{};
m_type{0},
m_exits{0},
m_path{""},
m_texturepath{""}
}
}[/quote]
You used the {} at the end facing the same direction thus you opening another scope and never close them, so that’s i’m dead certain won’t work.Also notice the ; and , mixed in the parameter list… That code won’t compile on any compiler

You’re right about the typos, and round brackets do work just fine… it’s just that Stroustrup advises to use curly brackets, even in the constructor’s head because “The rules for {} initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.”. It works fine for me, and it takes a while for veterans to adapt.

You’re right about the typos, and round brackets do work just fine… it’s just that Stroustrup advises to use curly brackets, even in the constructor’s head because “The rules for {} initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.”. It works fine for me, and it takes a while for veterans to adapt.[/quote]

May i ask which compiler you use ? i NEVER EVER saw any newer or older libraries ever used curly brackets in initializing… and just tried in any possible way it won’t compile…

gcc 5.2.1
And you?

Ok as i see the Initializer lists are c++ 11 features.

[quote=“Modanung”]
And you?[/quote]

visual c++ 2008 and 2010 (but mostly 2008)