Page 1 of 1

string< T > Assignment operator

Posted: Wed Dec 19, 2007 8:45 am
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!

Posted: Wed Dec 19, 2007 2:20 pm
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).

Posted: Wed Dec 19, 2007 2:26 pm
by Acki
it looks good...
but what error message did you get?

Posted: Wed Dec 19, 2007 4:11 pm
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;
}

Posted: Wed Dec 19, 2007 5:20 pm
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:

Posted: Wed Dec 19, 2007 5:54 pm
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.

Posted: Thu Dec 20, 2007 3:44 am
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

Posted: Thu Dec 20, 2007 4:40 pm
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

Posted: Fri Dec 21, 2007 4:27 am
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)

Posted: Fri Dec 21, 2007 1:18 pm
by Acki

Posted: Fri Dec 21, 2007 9:14 pm
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

Posted: Sat Dec 22, 2007 1:27 am
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