Solved

What's the problem with this Overloading statement?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 56
Question of running the program built from sample in codeproject website 3 61
countHi challenge 25 98
word0 challenge 3 77
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 …
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 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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

9 Experts available now in Live!

Get 1:1 Help Now