无法将argument1转换为常量字符
Cannot convert argument1 to const char
我有这个代码:
#include <iostream>
#include <string>
using namespace std;
typedef unsigned char byte;
byte text[] = "test";
int text_len;
struct Huf {
byte id;
int wh;
Huf *left, *right;
};
struct List {
List *next;
Huf *tree;
};
List *head;
char code[256];
void createList();
void writeList();
void delList(List *);
void addList(Huf *);
Huf *findDels();
void createTree();
void rlrootTree(Huf *, unsigned);
int main()
{
text_len = strlen(text);
createList();
writeList();
createTree();
cout << "writeCodesn";
rlrootTree(head->tree, 0);
cout << endl;
return 0;
}
void createList()
{
int i;
int ch[256] = { 0 };
for (i = 0; i<text_len; i++) ch[text[i]]++;
List *l;
Huf *h;
head = 0;
for (i = 0; i<255; i++) if (ch[i]>0)
{
h = new Huf;
h->id = i; h->wh = ch[i];
h->left = 0; h->right = 0;
l = new List;
l->tree = h;
l->next = head; head = l;
}
}
void writeList()
{
cout << "writeListn";
List *l = head;
while (l)
{
cout << (l->tree)->id << " ";
l = l->next;
}
cout << endl;
l = head;
while (l)
{
cout << (l->tree)->wh << " ";
l = l->next;
}
cout << endl;
}
void delList(List *l)
{
List *lp, *lc;
if (l == head) { head = l->next; delete l; }
else
{
lp = head; lc = lp->next;
while (lc != l) { lp = lc; lc = lc->next; }
lp->next = lc->next; delete lc;
}
}
void addList(Huf *h)
{
List *l = new List;
l->tree = h;
l->next = head;
head = l;
}
Huf *findDels()
{
List *l = head, *sm = head;
Huf *h;
while (l)
{
if ((l->tree)->wh < (sm->tree)->wh) sm = l;
l = l->next;
}
h = sm->tree;
delList(sm);
return h;
}
void createTree()
{
Huf *h, *h1, *h2;
while (head->next)
{
h1 = findDels();
h2 = findDels();
h = new Huf;
h->id = ' '; h->wh = h1->wh + h2->wh;
h->left = h1; h->right = h2;
addList(h);
}
}
void rlrootTree(Huf *h, unsigned index)
{
if (h)
{
code[index] = '0';
rlrootTree(h->right, index + 1);
if (h->left == 0)
{
code[index] = ' ';
cout << h->id << "->" << code << " ";
}
code[index] = '1';
rlrootTree(h->left, index + 1);
}
}
但我收到了这个错误消息:
Error 1 error C2664: 'size_t strlen(const char *)' : cannot convert argument 1 from 'byte [5]' to 'const char *' c:usersuserdocumentsvisual studio 2013projectstest11test11source.cpp 35 1 test11
2 IntelliSense: argument of type "byte *" is incompatible with parameter of type "const char *" c:UsersuserDocumentsVisual Studio 2013Projectstest11test11Source.cpp 35 20 test11
我不知道为什么?
typedef unsigned char byte;
byte text[] = "test";
...
strlen(text);
最终转变为
unsigned char text[] = "test";
属于const unsigned char *
类型,而strlen
不接受该类型。
size_t strlen ( const char * str );
您可以将text
数组定义为signed char
的数组,或者简单地定义为char
,也可以在使用调用strlen
时强制转换它
strlen((const char*)text);
// or
reinterpret_cast<const char*>(text);
解决方案1:将字节数组强制转换为char*
:
text_len = strlen(reinterpret_cast<char*>(text));
解决方案2:如果您可以按如下方式更改typedef,它将在不进行强制转换的情况下工作:
typedef char byte; // the compiler then can implicitely convert byte* to char*
问题是无符号:如果您的C++实现将char
处理为有符号(是的,这取决于实现!(,编译器将不知道您希望如何从无符号(例如char 253(转换为等效的有符号。
相关文章:
- 如何循环访问常量字符**?
- constexpr 函数获取常量字符*
- 如何计算常量字符**中的总字符数?
- QDial:如何将 int 值传递给需要常量字符*的 Qfile?
- 为什么 std::string_view 比常量字符*快?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 在编译时将常量字符* 转换为常量 char_type*
- 将字符串数组传递给接受常量字符**的函数
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 字符串强制转换为常量字符*
- 使用常量字符*时退出代码 139
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- 无法<string>从"常量字符 []"转换为<类名>
- 初始化常量字符* 数组
- 常量字符*的性质是什么?
- C++ 获取向量中常量字符* [ ] 的长度
- 无法将参数 1 从 WCHAR 转换为常量字符 *
- 如何在 c++ 中将向量转换为<string>常量字符* const*?
- C++将常量字符* 指针数组传递给对象