c++ - 无法将参数从 `const char *` 转换为 `char *`

标签 c++ string

给定这段代码:

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 * to char *

为什么?

最佳答案

您正在调用的函数需要一个指向可修改缓冲区的指针,char*。您正在传递一个指向不可修改缓冲区的指针,const char*

由于您的 member 函数不修改其输入,您应该更改其声明以接收 const char*

member::member(const char *ip)

关于c++ - 无法将参数从 `const char *` 转换为 `char *`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7791330/

相关文章:

c++ - 用C++读取文件

PHP - 如何生成带有控制字符或二进制数据的字符串?

c# - 在一行中将对象列表转换为字符串

c++ - 取消引用不适用于集合中的智能指针

c++ - if..else.. 语句未输出预期结果。 while..循环问题?

c++ - 是否可以将对指针的引用从 Excel VBA 传递到 C++?

java - 如何返回字符串中x个字符之后的所有内容

javascript - JS正则表达式替换字符串中的数字

javascript - String.match 正则表达式中的变量

c++ - 使用 Media Foundation SDK 进行直播