Reputation:
Given this code:
void group::build(int size, std::string *ips){
/*Build the LL after receiving the
member list from bootstrap*/
head = new member("head");
member *temp1,*temp2;
temp1 = new member(ips[0].data()); // error here
// ....
}
member::member(char *ip){
strcpy_s(this->ip_addr,sizeof(ip),ip);
this->next = NULL;
this->previous = NULL;
}
And a pointer to string defined as:
std::string *ips;
I want to initialize the array, ips
with strings but when I try to get the char data from any array member I get the error:
cannot convert parameter from
const char *
tochar *
Why?
Upvotes: 3
Views: 12640
Reputation: 56479
Change line
member::member(char *ip)
to
member::member(const char *ip)
and, i'm not sure about your usage of strcpy_s
Upvotes: 1
Reputation: 26920
std::string data has this signature
const char* data() const;
You are trying to call the member c'tor with which expects char * so here is your error.
Change this : member::member(char *ip)
To this : member::member(const char *ip)
Upvotes: 1
Reputation: 361254
Change this:
member::member(char *ip)
to this
member::member(const char *ip)
That is, you've to change the parameter type of the constructor.
Alternatively, which is also a better solution, simply make the parameter const std::string &
:
member::member(const std::string &)
This approach lets use better interfaces provided by std::string
class. These interfaces are much more better than C-string interfaces such as strcpy
, strlen
, strcat
etc.
Upvotes: 1
Reputation: 612794
The function you are calling expects a pointer to a modifiable buffer, char*
. You are passing a pointer to a non-modifiable buffer, const char*
.
Since your member
function does not modify its input you should change its declaration to receive const char*
.
member::member(const char *ip)
Upvotes: 4
Reputation: 991
Because member::member is defined to take char * as a parameter, and string.data() is giving a const char * as a value (since it is returning a reference to its own internal memory). You could use const_cast or change member::member method signature.
Upvotes: 1
Reputation: 22010
Well, data()
returns a const char*
, hence the error. You should change member::member
to receive a const char*
, as you're copying its value anyway.
Also, note that sizeof(ip)
is equal to sizeof(char*)
, which is just a size of a pointer. You should pass the size of the this->ip_addr
buffer instead.
Upvotes: 1