C++ 无法将参数1转换为常量字符

C++ 无法将参数1转换为常量字符,c++,pointers,c++11,C++,Pointers,C++11,我有以下代码: #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

我有以下代码:

#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 << "writeCodes\n";
    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 << "writeList\n";
    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] = '\0';
            cout << h->id << "->" << code << " ";
        }
        code[index] = '1';
        rlrootTree(h->left, index + 1);
    }
}

。。。我不知道为什么?

解决方案1:将字节数组强制转换为
char*

text_len = strlen(reinterpret_cast<char*>(text));

问题是无符号:如果C++实现了按代码签名的代码> char < /代码>(是的,这是依赖于实现的),编译器就不知道如何从一个无符号(例如char 253)转换成一个签名的等价物。p>

typedef unsigned char byte;
byte text[] = "test";
...

strlen(text);
最终变成

unsigned char text[] = "test";
类型为
const unsigned char*
,并且
strlen
不接受该类型

size_t strlen ( const char * str );
您可以将
文本
数组定义为
有符号字符
的数组,或者只定义
字符
,也可以在调用
strlen时使用

strlen((const char*)text);
// or
reinterpret_cast<const char*>(text);
strlen((常量字符*)文本);
//或
重新解释(文本);

“const unsigned char*”不是“const char*”(您可以在这里重新解释)我非常感谢并抱歉我问了这么一个愚蠢的问题,非常感谢!信息技术就这些fixed@YordanChimev“一切都已修复”我严重怀疑
reinterpret\u cast
除了消除编译器错误/警告之外,还能修复任何东西@DieterLücking在这种情况下,
static_cast
不是更合适吗?注意:char是特殊的:“char”、“signed char”和“unsigned char”是三种不同的类型-不适用于“short”、“int”和。。。
size_t strlen ( const char * str );
strlen((const char*)text);
// or
reinterpret_cast<const char*>(text);