Re: string class

From: Henrik Stuart (hstuart@geocities.com)
Date: 02/09/02


Greetings,

On Saturday, February 09, 2002 5:00:49 PM Karl B Buchner wrote:
> I am using the following code to show a character a room:
[snip]
>  *this << buf.pointer;

> *this << buf.pointer should send the contents of buf to the character
> (*this << "Huh?\r\n"; works fine)

std::string::pointer is:
 - type of character pointers
 - it is equivalent to allocator_type::pointer
 - for type std::string, it is equivalent to char*

 Hence, you're trying to output a type, namely char*, rather than what
 your buffer is containing. :o)

> I get the following compile error, however:

> C:\Dynamic\src\character.cpp(268) : error C2274: 'function-style cast' :
> illegal as right side of '.' operator

The above results in the compiler trying to use a function-style cast.
:o)

> I looked at the documentation for the error, and it says I should use the
> operator keyword
> before pointer, but buf.operator pointer causes a ton of errors.

There are some 48 operators in C++ of which most can be overloaded,
pointer is not one of them. So that makes little sense, and I
speculate you read the documentation a tad wrong in this case, not
that I could outright find it, a compiler would be helpful to
pinpointing the problem. :o)

> Any help appreciated,

Output the contents of the buffer instead. :o)
As was pointed out in another post by Juliano Ravasi Ferraz:
Output using either operator<<(const std::string&) or operator<<(const
char*) (which you should/would have in your character class) doing:
*this << buf;
or
*this << buf.c_str();

(.c_str() returns a const char* null-terminated string which is
correct _for as long as you do not modify your string_!!! - remember
this). :o)

Now, whether to use std::string or not... std::string works somewhat
like a normal dynamic array which doubles its' length when its' limit
is reached and halves it once it's a quarter full, give or take a bit
depending on the specific implementation.
When you have something where you're not sure exactly how much you're
going to output using std::ostringstream might be a benefit, yet it
might not. :o)
Investigate both and consider which you prefer.

--
Yours truly,
  Henrik Stuart (http://www.unprompted.com/hstuart/)

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT