给定这段代码:
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;
}
指向字符串的指针定义为:
std::string *ips;
我想用字符串初始化数组 ips
但是当我尝试从任何数组成员获取 char 数据时出现错误:
cannot convert parameter from
const char *
tochar *
为什么?
最佳答案
您正在调用的函数需要一个指向可修改缓冲区的指针,char*
。您正在传递一个指向不可修改缓冲区的指针,const char*
。
由于您的 member
函数不修改其输入,您应该更改其声明以接收 const char*
。
member::member(const char *ip)
关于c++ - 无法将参数从 `const char *` 转换为 `char *`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791330/