Avatar of prain
prain
Flag for United States of America asked on

What's the problem with this Overloading statement?

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 );
Programming Languages-OtherC++

Avatar of undefined
Last Comment
prain

8/22/2022 - Mon
evilrix

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;
}
prain

ASKER
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()
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
prain

ASKER
Thank You!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23