Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What's the problem with this Overloading statement?

Posted on 2012-03-19
4
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

721 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