Invalid conversion from const char* to char error
You should be using single quotes for characters. Double quotes means you're using a (potentially single-character) string literal, which is represented as a const char *
(pointer to constant character).
Correct syntax: circle1.symbol = '*';
invalid conversion from 'const char*' to 'char*'
Well, data.str().c_str()
yields a char const*
but your function Printfunc()
wants to have char*
s. Based on the name, it doesn't change the arguments but merely prints them and/or uses them to name a file, in which case you should probably fix your declaration to be
void Printfunc(int a, char const* loc, char const* stream)
The alternative might be to turn the char const*
into a char*
but fixing the declaration is preferable:
Printfunc(num, addr, const_cast<char*>(data.str().c_str()));
C++ invalid conversion from 'const char*' to char*
You are doing an exercise, did the course you are following not explain what you are supposed to do?
I would guess that this is an exercise in dynamic memory allocation and what you are expected to do is use strcpy
after you have allocated some memory so that you have somewhere to copy the string to. Like this
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
You need to allocate one extra char because C style strings are terminated with an additional nul byte.
But any C++ programmer who was doing this for real (instead of it being a learning exercise) would use a std::string
as paxdiablo says.
Error : invalid conversion from 'const char*' to 'char*' [-fpermissive]
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
C++ - Error Conversion from const char* to char*
The right way to this is:
#include <cstring>
#include <string>
#include <stdio.h>
bool validURL(const char *url); // Must forward declare or prototype the
// function before calling
int main()
{
validURL("www.why_CPP_hates_me.com"); // Single quotes are for single chars
// Double quotes are for string
// literals, which create a const char*
return 0;
}
bool validURL(const char *url)
{
const char *q = strchr(url, '?'); // You have to assign to a const char*
// to maintain const correctness.
// strchr() has const char* and char*
// overloads. But you have to assign to
// the correct type.
return true;
}
Or if you wanted to you could do this:
bool validURL(const char *url)
{
char *q = strchr(const_cast<char*>(url), '?');
// Casting away const is not recommended generally. Should be careful.
return true;
}
invalid conversion from ‘const char*’ to ‘char*
Well that's because c_str()
returns a const char *
and not a char *
Just change the line to:
const char * c = s.c_str();
and the function declaration to
std::string Exec(const char* cmd)
Like noted before and you're good to go.
See it live here.
Invalid conversion from 'const char*' to 'char'
First of all (I think this is what you intended to write)
int readSeating (char, vector<char>&);
should be
int readSeating (char*, vector<char>&);
^^^
expect an lvalue
for the first argument. What that means is simply something that it can possibly assign to.
However you are calling it as
readSeating("SeatingChart.txt", seating);
by passing a temporary for the first argument. The complier wants to know that you will not modify it inorder to let you pass a temporary. You can tell it that you wnot modify it by declaring the function like.
int readSeating (const char*, vector<char>&);
^^^^
Same goes for
int readSeating(char myFile, vector<char>& vect)
to
int readSeating(const char* myFile, vector<char>& vect)
I suggest you should go ahead and change them to strings
.
int readSeating (string, vector<char>&);
int readSeating(string myFile, vector<char>& vect)
Related Topics
Static Constexpr Member of Same Type as Class Being Defined
Change Default Value of Cmake_Cxx_Flags_Debug and Friends in Cmake
C++11 Lambda Implementation and Memory Model
Why Can't I Define a Function Inside Another Function
Why Static Variable Needs to Be Explicitly Defined
How to Construct Std::Array Object with Initializer List
Emulating Shifts on 32 Bytes with Avx
How to Combine Several C/C++ Libraries into One
How to Expand Call to Variadic Template Base Classes
What Is the Optimal Algorithm for Generating an Unbiased Random Integer Within a Range
What Are the Issues with a Vector-Of-Vectors
C++ Preprocessor: Avoid Code Repetition of Member Variable List
What Is the Idiomatic Way in Cmake to Add the -Fpic Compiler Option
Does Boost Have a Datatype for Set Operations That Is Simpler Than the Stl
How Would I Load a Png Image Using Win32/Gdi (No Gdi+ If Possible)