string< T > Assignment operator

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

string< T > Assignment operator

Post by pippy »

I have some problems assigning one string to another. The string is in it's a custom namespace, and is private:

Code: Select all

namespace ExampleNameSpace{
    class Example {
    	public:
    		Example ();
    		~Example ();

            // problem is here:
            void setName(core::string<char> str) {
                 name = str;
            };
            core::string<char> getName() {return name; };
    	protected:
            core::string<char> name;
    };
}
It crashes upon trying to assign str to name. Why does it do this? I shouldn't have to use C style char* pointers!
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Maybe you have no valid object of Example?
Otherwise it seems fine. It would be even better if you use a const reference as parameter for setName (a little bit faster).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

it looks good...
but what error message did you get?
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

What are you doing differently from this (compilable, working) example?

Code: Select all

#include <irrlicht.h>

using namespace irr;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

namespace ExampleNameSpace
{
class Example
{
public:
    Example() { }
    ~Example() { }

    void setName(core::string<char> str) { name = str; }
    core::string<char> getName() { return name; }

protected:
    core::string<char> name;
};
};


int main()
{
    ExampleNameSpace::Example foo;
    core::string<char> str("Error is between keyboard and chair.");

    foo.setName(str);
    
    printf("%s\n", foo.getName().c_str());

	return 0;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

oh, wait, if this is the original code you're using then there are the bracets missing from de-/constructor:

Code: Select all

namespace ExampleNameSpace{
    class Example {
       public:
          Example () {}; // <<<<< this bracets are missing
          ~Example () {}; // <<<<< this bracets are missing

            // problem is here:
            void setName(core::string<char> str) {
                 name = str;
            };
            core::string<char> getName() {return name; };
       protected:
            core::string<char> name;
    };
}
but then you should get "undefined reference" errors !!! :shock:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I know, and it's pretty tiresome having to fix build problems in code snippets in order to investigate runtime problems. Clearly the code that pippy provided isn't the same source that's generating his/her/its runtime error.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

Rogerborg: you where right, the error was between keyboard and chair.

In another part of my program I had this:

Code: Select all

   #pragma pack(1) 
    struct MAPMATRIX{
    	unsigned int width;	
    	unsigned int height;
    	unsigned short cellsize;
    };
The pragma pack caused the protected variables order in the Example class to be changed. Mixing C and C++ is bad
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Mixing C and C++ is bad
This has nothing to do with C/C++ interoperability. You have created a binary compatibility problem.

If you are going to use the pack pragma you need to restore it to the default. You would usually do this with #pragma pack (push, 1) and then a #pragma pack (pop).

Travis
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

Vitek: so my struct would look like this?

#pragma pack (push, 3)
struct MAPMATRIX{
unsigned int width;
unsigned int height;
unsigned short cellsize;
};
#pragma pack (pop)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Acki, how likely is it that the OP is using the IBM VisualAge C++ compiler? I'd guess that it is _very_ unlikely. It is much more likely that he is using MSVC. The best way to find out is to ask. I bring this up because the functionality is different between compilers, and you link to documentation that is most likely wrong for this user. If he is indeed using the Microsoft compiler, here is the documentation for that.

pippy, what you have posted is pretty close. I don't know if why would possibly want 3 byte packing, but that isn't a supported value [only 1, 2, 4, 8 and 16 are valid]. I think you want this...

Code: Select all

#pragma pack (push, 1)
// declare types with 1 byte packing
#pragma pack (pop)
You should note that modifying the packing of structures can result in worse performance than if you had left the packing at the default.

Travis
pippy
Posts: 49
Joined: Sun Jul 08, 2007 11:31 pm

Post by pippy »

Thanks, vitek your a life saver =D. It works in both MSVS 8 and gcc

I like to use structs so reading in files becomes easier, but to be on the safe side usually I read in one value a time anyway
Post Reply