I'm trying to read a binary file stream into a std::vector buffer.
std::ifstream file(srcPath, std::ifstream::binary);
file.unsetf(std::ios::skipws);
const std::vector<unsigned char> buffer(bufferSize);
file.read(buffer.data(), bufferSize);
But I get the following error
Cannot initialize a parameter of type 'std::__1::basic_istream >::char_type *' (aka 'char *') with an rvalue of type 'const std::__1::vector >::value_type *' (aka 'const unsigned char *')
I'm having trouble interpreting this error and figuring out what I'm doing wrong in my call to read.
const std::vector<unsigned char> buffer(bufferSize);
You declared a const object here. By definition, a const object cannot be modified. Your plans to modify this object, by reading something into it, are already doomed to a big, abysmal, failure at this point. But there's also a second problem.
file.read(buffer.data(), bufferSize);
If you actually read your compiler's error message, slowly, it tells you exactly what the problem is.
First of all, read()s first parameter is a char *, a pointer to a character.
But you are passing a const unsigned char *. That's because data(), given that buffer is const, obviously returns a const unsigned char *.
And that's why you get a compilation error. If you now reread the compilation error, skipping about half of its words, it makes perfect sense now:
Cannot initialize a parameter of type ... 'char *' with an rvalue of
type ... 'const unsigned char *'
To fix it, your buffer should not be a const object. Preferrably, it should be a std::vector<char>, so you end up really passing a char * to read().
But, if you insist, you can keep it a vector of unsigned chars and use reinterpret_cast to cast the result of data() from unsigned char * to char *.
You made your vector const.
So, you cannot change its contents.
You want to change its contents (that's its purpose).
So, don't make it const.
Related
first of all this is not duplicate one because same error Question
asked before but in different context
1
code
#include<iostream>
#include<cstring>
int main()
{
const char *s1;
const char *s2="bonapart";
s1=new char[strlen(s2)+1];
strcpy(s1,s2);
std::cout<<s1;
}
Output
[Error] invalid conversion from 'const char*' to 'char*' [-fpermissive]
Why such error ?
If I replace const char *s1 by char *s1 compile fine. But I think this const only saying that s1 is pointing to constant string means we can't modify that string whom it is pointing. It does not mean that s1 is constant ptr. please put some light on this.
Why such error ?
Look at the declaration of strcpy:
char* strcpy( char* dest, const char* src );
Pay particular attention to the first parameter. And very particular attention to the type of the first parameter: char*. It isn't const char*.
Now, look at the type of the argument that you pass.
const char *s1;
It's not char*. It's const char*. You cannot pass a const char* into a function that accepts char* because former is not convertible to the latter, as the diagnostic message explains.
But I think this const only saying that s1 is pointing to constant string means we can't modify that string whom it is pointing.
That's exactly what const bchar* means. So, how do you expect strcpy to modify the content of the pointed string when you know that it cannot be modified? Technically in this case the pointed string isn't const, it's just pointed by a pointer-to-const
As you say, const char *s1; means that the string pointed at by s1 is not modifyable.
On the other hand, strcpy(s1,s2); will modify the string pointed at by s1.
This is against the condition "the string pointed at by s1 is not modifyable".
Definition of method is:
void setArgument(char *);
And i call that method with this code:
setArgument("argument");
But my VisualStudio compiler gets me the next error:
cannot convert argument 1 from 'const char [10]' to 'char *'
Is it possible to send arguments like this or I must change arguments type in the method?
Also, VS show me next note in output: note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
The problem is that string literals are arrays of constant characters.
While the array could easily decay to a pointer to its first element, the type of that pointer is const char *. Which needs to be the type of the argument for your functions.
And if you need to modify the string you pass, then you should create your own non-constant array:
char argument[] = "argument";
setArgument(argument);
Of course, since you're programming in C++ you should stop using char pointers and arrays and instead use std::string.
It's possible, just if you really need the argument to be mutable (char* and not char const*), you need to allocate a new storage in the mutable memory and clone the contents of the constant memory to there, if that fits into your definition of "convert".
auto const len = strlen(input);
auto const buf = std::unique_ptr<char[]>(new char[len + 1]);
memcpy(buf, input, len + 1);
If you actually need char const* and if you are C++17 or later, you can possibly change the signature to setArgument(std::string_view arg), making it misuse-proof.
I am trying to write a Fopen statement like this:
FILE *fp;
fp = fopen("client." + receiver->get_identifier().c_str() + ".vol", "a+");
where receiver->get_identifier() returns a string. However, I am getting the error in the title. I read the question here but wasn't having any luck because the first argument of fopen is const char*. What do I have to change to get this to compile?
receiver->get_identifier().c_str()
returns a const char*, not a std::string, so operator+ cannot kick in (one argument of it must be a std::string). Removing the c_str() and converting at the end with std::string::c_str() should do the trick
fopen(("client." + receiver->get_identifier() + ".vol").c_str(), "a+");
This is because you'd have a const char* plus a std::string, and operator+ will work.
If you may wonder why one cannot define an operator+ for const char*s, that's because C++ does not allow operator overloading for fundamental types; at least one argument must be of a user-defined type.
Try changing the first argument to
(string("client.") + receiver->get_identifier() + ".vol").c_str()
This will add std::string objects with C-Style strings, which can be done, and only take the character pointer at the end (via .c_str()). Your code now tries to add C-Style strings, which is not possible.
The following code produces the compilation error, invalid conversion from 'const char*' to 'char*'. Both ptrInputFileName and ptrFileName are both declared as const char*. Any suggestions on how to get this to compile? Thank you.
TextInputBuffer::TextInputBuffer(const char *ptrInputFileName)
: ptrFileName(new char[strlen(ptrInputFileName) + 1])
{
//--Copy the file name.
std::strcpy(ptrFileName, ptrInputFileName);
Obviously, you can't copy into a something pointed to by a const * - remove the const, and if that cause problems calling the constructor, you are doing something semantically invalid.
Also, your use of pseudo-hungarian makes the code difficult to read. Lose the ptr prefixes, and make the names shorter.
strcpy takes the the destination as non-const pointer char* and ptrFileName is const char*. No implicit conversion possible and I wouldn't recommend an explicit conversion. Just make ptrFileName non-const.
Use std::string.
struct TextInputBuffer {
TextInputBuffer(const char *filename)
: _filename(filename)
{}
// Explicit delete not even required as it would have been
// when you used new.
private:
std::string _filename;
};
I have some code written by someone else in which some functions take arguments whose data types are followed by a *&. I'm used to functions taking one or the other, e.g. taking a "double *" or a "double &" but not both. It would have thought they would just cancel out.
Here's an example, and this is all from their code which supposedly works:
In a header file there's a function declared as:
void someClass::foo(const unsigned int*& ubuff);
Then in my main file, there's a pointer to some UINTs declared and initialized as:
unsigned int* pbuff = new UINT[n];
Then, the function someClass::foo is called as:
foo(pbuff);
When I compile, I get the error "cannot convert parameter 1 from 'unsigned int *' to 'const unsigned int *&'". If I change my function call to the following, it compiles ok:
foo((const unsigned int *&)(pbuff));
So, my questions are:
What is the purpose of the *&? I.e., how is that different from * or & by themselves, and why do they not just cancel themselves out? And what does that make the function foo expect? An address? A value? The address of an address? It's confusing.
Is the fix of simply casting a "unsigned int*" to a "const unsigned int*&" ok or do I need to do something else?
Just another quick example, and this is from completely within a cpp file distributed by someone else, so I assume this compiled for them. They have a function call as
klabels = new int[sz];
EnforceLabelConnectivity(klabels, m_width, m_height, nlabels, numlabels, double(sz)/double(STEP*STEP));
When I try to build, I get an error "'SLIC::EnforceLabelConnectivity' : cannot convert parameter 1 from 'int *' to 'const int *&'".
Thanks as always.
For a type T, T *& means "a reference to a pointer to a T." The calling function should pass a pointer to a T. As to the const modifier, try foo((const unsigned int *)pBuff); or else declare pBuff to be a pointer to a const int to start with.
Well, I tried casting to a (const unsigned int *), and that did not work (wouldn't build). In the end, I had to fully cast to a (const unsigned int *&) in the foo function example in my original post. Hope this helps. Thanks.
As an aside, the reason I did not go the route of making pbuff const from the beginning in my main code is that it is passed to another function later on which does modify it, so I don't think it would work to have it made const when declared.
Pointers and references are really differents, but using a reference to a pointer is kind of useless.
This is not ok, pbuff is not const that's why your code doesn't compile. Use a const pbuff or change your prototype, casting like this is really ugly