Solved

What's the problem with this Overloading statement?

Posted on 2012-03-19
4
362 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 150 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
data scientists and AI 17 74
How to gracefully close the c++ 11 thread? 3 67
count11 challenge 6 48
gdb doesn't stop on breakpoint 2 13
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now