Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What's the problem with this Overloading statement?

Posted on 2012-03-19
4
Medium Priority
?
372 Views
Last Modified: 2012-03-19
friend BinaryStream& operator<<(BinaryStream &bStream,   const std::string& inString );


BinaryStream& operator<<(BinaryStream &bStream,   const std::string& inString )
    {
        std::string tempString(inString);

****        write(reinterpret_cast<char*>(&tempString),  inString.length());

        return bStream << inString;
    }

I am getting an error :
BinaryStream.cpp:193: error: invalid conversion from ‘char*’ to ‘int’
BinaryStream.cpp:193: error: invalid conversion from ‘long unsigned int’ to ‘const void*

The write function has the following header:
void write( const char* buffer, int buffersize );
0
Comment
Question by:prain
  • 2
4 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 37739193
Well, for a start you can't just cast a std::string to a char * like that. It's a nonsensical cast since there is no cast operator supported that will give you a pointer to the strings internal string buffer. If you are using C++11 you can get a mutable pointer to the strings internal buffer using &inString[0] but this is unsafe in earlier versions of C++. For them you'd have to use a vector as a buffer.

Secondly, what are you trying to do read or write? You seem to have a mixture of semantics going on. This should be a write, in which case this should be all you need.

BinaryStream& operator<<(BinaryStream &bStream,   const std::string& inString )
{
        return bStream << inString;
}
0
 

Author Comment

by:prain
ID: 37739302
Well sorry for writing the question wrongly. This is the one that I am getting an error.

BinaryStream& operator<<(BinaryStream &bStream,   const std::string& inString )
    {
        std::string tempString(inString);

****        write(reinterpret_cast<char*>(&tempString.c_str()),  inString.length());

        return bStream;
    }

Actually what wew do is creating a wrapper class called BinaryStream in which we are maintaining a stringstream object. So the write() function above simply use the stringstream.write().

I am still getting an error at **** inspite of using c_str()
0
 
LVL 86

Accepted Solution

by:
jkr earned 600 total points
ID: 37739362
The cast is not required and erroneous, since you are using the address of a 'const char*' - did you mean

BinaryStream& operator<<(BinaryStream &bStream,   const std::string& inString )
    {
        std::string tempString(inString);

        write(tempString.c_str(),  inString.length());

        return bStream;
    }

Open in new window


?
0
 

Author Closing Comment

by:prain
ID: 37739445
Thank You!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question