Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » String to std::string conversion
String to std::string conversion [message #32848] Wed, 15 June 2011 03:12 Go to next message
GaroRobe is currently offline  GaroRobe
Messages: 30
Registered: May 2011
Location: Khabarovsk, Russia
Member

Hello.

I have a code:
std::string tempPath;
FileSel file;

{...}

tempPath = file[i]; <<== error C2593: 'operator =' is ambiguous


What would that possibly mean?
Re: String to std::string conversion [message #32849 is a reply to message #32848] Wed, 15 June 2011 05:25 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 526
Registered: March 2007
Contributor
Please test if

tempPath = const_cast<String&>(file[i]);


compile.

If yes, I believe the ambiguity arises because of

	operator std::string() const;

And
     operator const char *() const   { return Begin(); }


Re: String to std::string conversion [message #32850 is a reply to message #32849] Wed, 15 June 2011 05:35 Go to previous messageGo to next message
GaroRobe is currently offline  GaroRobe
Messages: 30
Registered: May 2011
Location: Khabarovsk, Russia
Member

Quote:
Quote:

C:\MyApps\proto1\main.cpp(42) : error C2872: 'String' : ambiguous symbol
could be 'c:\myapps\opencv22\opencv2/core/core.hpp(84) : std::string cv::String'
or 'c:\upp\uppsrc\core\String.h(294) : Upp::String'
C:\MyApps\proto1\main.cpp(42) : error C2440: 'const_cast' : cannot convert from 'Upp::String' to 'cv::String &'
Reason: cannot convert from 'Upp::String *' to 'cv::String *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Re: String to std::string conversion [message #32851 is a reply to message #32848] Wed, 15 June 2011 07:16 Go to previous messageGo to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
Hello, Artem.

Try this:
std::string tempPath;
FileSel file;

String path;
if (file.ExecuteSelectDir())
	path = ~file;

// Conversion from UPP::String to const tchar * to std::string
if (!path.IsVoid())
	tempPath = ~path;
Re: String to std::string conversion [message #32852 is a reply to message #32850] Wed, 15 June 2011 07:30 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi GaroRobe

Lances explanation about the const char* and std::string is correct. However, his solution is not, the const_cast can only change the constness of variable, not its type. Also, casting Upp::String to std::string is not a good idea anyway, as their internal representation probably differs.

Anyway, there is quite a number of possible solutions:
std::string tempPath;
FileSel file;
{...}
tempPath = ~file[i]; // converts the Upp::String to const char*
//OR
tempPath = file[i].Begin(); // basically the same as above
//OR
tempPath = std::string(~file[i],file[i].GetLength()); // create a new std::string with the same content and length
//OR
tempPath = std::string(~file[i],file[i].GetLength()); // again, more verbose variation on the previous line

Not that the first two solutions contain potential bug. If there are zero bytes ('\0') in the string, only part up until the first null would get copied. The last two solutions always copy the entire string properly, so I would recommend you to use one of those Wink


Best regards,
Honza
Re: String to std::string conversion [message #32853 is a reply to message #32852] Wed, 15 June 2011 07:37 Go to previous messageGo to next message
GaroRobe is currently offline  GaroRobe
Messages: 30
Registered: May 2011
Location: Khabarovsk, Russia
Member

Well, It compiles. But another problem arises.
My following lines are
tempFrame = imread( tempPath );
		if( findChessboardCorners( tempFrame, checkboardSize, foundCorners ))   <<=== Crashes here
		{
			frames.push_back( tempFrame );
			imagePoints.push_back( foundCorners );
		}

According to debugger, tempFrame is empty after imread() which is not what I expect.
BTW, that's why I am forced using STL types - most of parameters in OpenCV are std::something.

P.S.: Sorry, I know it's not about stl anymore - feels like I stumble at such topic shifts every time, but...

[Updated on: Wed, 15 June 2011 07:47]

Report message to a moderator

Re: String to std::string conversion [message #32854 is a reply to message #32853] Wed, 15 June 2011 07:51 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

GaroRobe wrote on Wed, 15 June 2011 07:37

BTW, that's why I am forced using STL types - most of parameters in OpenCV are std::something.
Well, maybe we should better come up with some solution to use OpenCV with U++ types, instead of fixing every single use separately Smile I'll have a look into it...

Honza
Re: String to std::string conversion [message #32855 is a reply to message #32854] Wed, 15 June 2011 07:56 Go to previous messageGo to next message
GaroRobe is currently offline  GaroRobe
Messages: 30
Registered: May 2011
Location: Khabarovsk, Russia
Member

A wishful thought... can't see how to implement it other than add conversion operators for each and every respective type and datastructure.
Re: String to std::string conversion [message #32857 is a reply to message #32853] Wed, 15 June 2011 08:07 Go to previous messageGo to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
Seems, you wanted the path to selected file instead of path to selected directory.

Please, look on the following example:
#include <CtrlLib/CtrlLib.h>

using namespace Upp;

GUI_APP_MAIN
{
	FileSel fs;

	std::string stdPath;

	if (fs.ExecuteOpen())
	{
		String path = ~fs;

		PromptOK("From UPP\1::\1String:&\1" + path);

		stdPath = ~path;

		PromptOK("From std\1::\1string:&\1" + String(stdPath));
	}
}
Re: String to std::string conversion [message #32858 is a reply to message #32857] Wed, 15 June 2011 08:09 Go to previous message
GaroRobe is currently offline  GaroRobe
Messages: 30
Registered: May 2011
Location: Khabarovsk, Russia
Member

Correct me if I'm wrong, but file[i] returns exactly path to the i-th selected file. Is it not? (debugger complies)
Previous Topic: MSVC 10 to Upp conversion
Next Topic: HttpClient and language choice (HTTP_ACCEPT_LANGUAGE)
Goto Forum:
  


Current Time: Thu Mar 28 19:12:39 CET 2024

Total time taken to generate the page: 0.00924 seconds