LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2009, 01:00 AM   #1
jh7777
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
Assigning custom string to std::string in C++


Hi there

I wonder if anyone could help me with the following C++ question: I've been given a custom-made string class which handles string, wstring and bstr. It has a number of methods and assignment operators to convert to and from different types.

The app I work on compiles happily in VS6 and VS2008, but when trying to compile in Redhat (version 4.1.1 in Redhat 5.0), for example for the following code:
MYString yyy = "abc";
std::string xxx1;
xxx1 = yyy; // THIS LINE GIVES AN ERROR
string zzz1 = yyy; // THIS LINE WORKS FINE


The error I get on the 3rd line is:

../../src/test.cpp:104: error: ambiguous overload for â?~operator=â?T in â?~xxx1 = yyyâ?T
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/basic_string.h:485: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>:perator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/basic_string.h:493: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>:perator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] <near match>
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/basic_string.h:504: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>:perator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

Any help would be greatly appreciated.

Cheers
JH7777
 
Old 10-12-2009, 01:03 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i dont like using the string class
i prefer char *
anyway i thinnk you need to add the class type to the class in order to have a sucessful assignment
 
Old 10-12-2009, 12:27 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You need to define a conversion operator
Code:
operator std::string() const;
Although, I have to wonder *why* you want this custom string class. The generally accepted sane approach is to use a UTF-8 encoded std::string everywhere. It's understandable if you want to use std::wstring everywhere and do encoding/decoding into UTF-8 on I/O as well. Having multiple string encodings flying around is usually a recipe for disaster.
 
Old 10-12-2009, 11:09 PM   #4
jh7777
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the replies!!!

>> Although, I have to wonder *why* you want this custom string class.

Indeed a good question. I dislike custom string-classes, but have to support this for multi-platform / UTF-8 support (VS6, VS2008 and Unix)

>> You need to define a conversion operator
>> operator std::string() const;


Tried this, to no help ;(

I had operator
operator const string&() const;
already defined. I suspect that's why the following works:

MYString yyy = "abc";
std::string zzz1 = yyy; // THIS LINE WORKS FINE

And I don't understand why then the following doesn't work:

MYString yyy = "abc";
std::string xxx1;
xxx1 = yyy; // THIS LINE GIVES AN ERROR

Maybe one option would be to simply define
#define MYString std::string in g++
(if std::string supports UTF8 encoding in g++), but still the above error has me baffled.

Cheers
jh7777
 
Old 10-13-2009, 12:02 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Hmm. You're only allowed one compiler conversion and one user-defined conversion, but a conversion to const std::string & should do it. Do you also have a const char* conversion? If so, the two will conflict. It's strange that the copy constructor works.. they should either both work or neither work.

Code:
Maybe one option would be to simply define
#define MYString std::string in g++
(if std::string supports UTF8 encoding in g++), but still the above error has me baffled.
All versions of std::string support UTF-8 encoding. Well, not quite all.. If you have for some insane reason a really old 7-bit char computer this code needs to support. And by "really old", I mean like PDP-11 old.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
int to std::string lucky6969b Programming 1 06-05-2006 07:17 AM
casting std::string to void * lucky6969b Programming 1 03-17-2006 04:38 AM
SIGSEGV for std::string dragondad Programming 2 02-05-2006 11:26 AM
std::string in write lucky6969b Programming 2 12-20-2005 10:53 PM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration