getting error - cannot pass objects of non-POD types

Hi,
I made a string class for wide characters.
this is the code -

using namespace std;
class MyString
{
public:
wstring wStr;
MyString () {wStr = L"";}
MyString (wstring str) {wStr = str;}
};

int main()
{
wstring firstName = L"Vipul";
MyString myStr (firstName);
wprintf(L"%s", myStr.wStr);
return 0;
}

But it gives error - cannot pass objects of non-POD type class std::wstring through ‘…’; call will abort at runtime.

What are POD type classes and non-POD type classes?
And how to solve this? I want to make my string class platform independent.

Thanks,

Platform independant? This problem is not related to platform at all its related to C++ standard. There isnt enough code to tell. POD means plain old data. That means wprintf expect a pointer or a variable but think that wstring is not, it`s probably an object.

If you write this code:

string s;
printf ("%s", s);

You’ll get the same error.

You need

printf ("%s", s.c_str());

which is the call to the actual characters stored in the string.

I assume wstring works the same way though I’ve never used it.

Tim

Didn’t know about wstring, they are basic_string with a type wchar_t. So it all make sense to me now ;-) As time said you need to use str.c_str(). That being said that won’t work either because you need to use wprintf and not printf.

Why don’t you use std::cout << myString.wStr();

Or even better provide a << operator so you could do std::cout << myString << …

Well, I tried this -

#include
#include
#include
#include <stdarg.h>

using namespace std;

class MyString
{
public:
wstring wStr;
MyString () {wStr = L"";}
MyString (wstring str) {wStr = str;}
void Format(const wchar_t*lpszFormat, …);
};

void MyString::Format(const wchar_t* lpszFormat, …)
{
wchar_t szBuffer[256];
va_list args;
va_start(args, lpszFormat);
vswprintf(szBuffer, 256, lpszFormat, args);
va_end (args);
// Got the string in szBuffer, Now convert to a wchar_t*
wcscpy(const_cast<wchar_t*>(this->wStr.c_str()), szBuffer);
}

int main()
{
wstring firstName = L"Vipul";
wstring lastName = L"Bajaj";
MyString myStr (firstName);
wprintf(L"%s\n", myStr.wStr.c_str());
cout<<myStr.wStr.c_str()<<endl;
myStr.Format(L"%s*%s", firstName.c_str(), lastName.c_str());
wprintf(L"%s\n", myStr.wStr.c_str());

return 0;
}

This works in windows, gives perfect result.
But in QNX it is giving only first character ‘V’ in the result.
I want to make it work on both. What do I need to change?

I hope you don`t think that because a program works under windows that i means your program is well written and respect the standard, lol!

wcscpy(const_cast<wchar_t*>(this->wStr.c_str()), szBuffer);

This line is illegal. Read the documentation on c_str()

Maybe you have futur plan for this class, but at this time its totally useless. If you come from a Windows background the MFC CString::Format() method was nice because you didnt have to care about buffer size, but your Format method doesnt provide that feature since its going though a fixed size buffer. For a C++ programmer you class is misleading. Why write a class if c_str() is used all over the place.

Also cout<<myStr.wStr.c_str()<<endl; should be cout<<myStr.wStr <<endl;