Ironworks Gaming Forum

Ironworks Gaming Forum (http://www.ironworksforum.com/forum/index.php)
-   General Conversation Archives (11/2000 - 01/2005) (http://www.ironworksforum.com/forum/forumdisplay.php?f=28)
-   -   C++ - std::string & char* (http://www.ironworksforum.com/forum/showthread.php?t=90406)

LennonCook 06-24-2004 08:02 AM

<span style="color: lightblue">Basically, I have a very large program which is *almost* compiling: my only problems are with a line that looks like:
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">std::string input = temp;</pre>[/QUOTE]<span style="color: lightblue">Where temp is a char* . My compiler (Borland 5) tells me "Cannot convert char* to std::string" . I've tried changing the code to be:

</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">std::string input(temp);</pre>[/QUOTE]<span style="color: lightblue">
But Borland tells me "No match for string::string(char *)".
But when I try this silly little block of code that I found elsewhere (in response to another problem related to the same classes), it compiles without so much as even a warning.
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;cstring&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;

int main(){
char *t;
char u[50];
char *v;
string w;
string s;

t = &quot;this string is t, it is a very long string. much longer than 30 chars.\n&quot;;
strcpy(u, &quot;this is u, it is also a fairly long string.\n&quot;);
v = strdup(u);
v[8] = 'v';
w = &quot;and this is string w, it's not a char array like the rest.\n&quot;;

s = t;
s += u;
s += v;
s += w;

delete v;
cout &lt;&lt; s;
}</pre>[/QUOTE]<span style="color: lightblue">
So, is there any rhyme or reason to Borland's complaining? And, more importantly, is there any way to fix it?

philip 06-24-2004 10:44 AM

I don't have any c++ experience (not counting nwscript [img]smile.gif[/img] ) but what I often do when I get an error I don't get is use google with the exact code of the error, except for my own constants or declarations.

When I did that with your code I got to this site, under the 'C++ has scary error messages' it looks like a similar error to the one you have. Also the description of the error is good looking at the error message you've got.

BTW and this is purely a guess at it cause as I said I don't know much about c++ but those 2 declarations look so similar I doubt they do much different things. Again I don't know about c++ but other languages have convert functions maybe you could try those (if they're in c++ of course), cause right now it looks like you're trying to declare a char* as string (also from the msg) which IMHO doesn't look very if that's possible so easy.

Why I think the other piece of code works. It looks like that piece is adding characters to a string, instead of immediately equalling a string to a char*?

Vaskez 06-24-2004 11:30 AM

Firstly, I don't think you don't need std:: in front of string if you declare "using namespace std;" at the top

</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">string input( temp );</pre>[/QUOTE]seems like it should work, but according to my reference you need a const char* not a char* as the input. So you could try casting temp to a const char* first, or copying its value to an already declared const char*.

You could also try

</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">string input;
input = temp;</pre>[/QUOTE]Yes, I find that it does make a difference if you declare the variable on the same line or not.

Let me know if it doesn't work, I might have some suggestions, including checking out this site:

http://www.cppreference.com/cppstrin...l#Constructors

LennonCook 06-24-2004 09:07 PM

<span style="color: lightblue">Time for me to slap myself silly. I've been using a string class which I coded, which publically inherits from std::string . I needed to provide my own constructors, and the assignment operator is virtual. Fixed up those two problems by adding few lines to my class, and it now compiles.

I now need to test this thing very quickly because it's due on Monday. It's a CGI application, and so I need a webserver which can be set up to run a binary file server-side. Meaning, I either need someone who has such a webserver, or I need to figure out how to configure Apache... and then there's the user manual...


All times are GMT -4. The time now is 06:51 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved