简体   繁体   English

为什么我收到错误:无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char*&#39; 赋值?

[英]Why am I getting error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in assignment?

I can't get this program to compile correctly.我无法让这个程序正确编译。 It's for a program that's a singly linked list.它适用于一个单向链表的程序。 This particular function is giving me crap for not converting something in it to string but I cannot see it.这个特殊的函数让我没有将其中的内容转换为字符串,但我看不到它。 I got help from someone who told me to fix another problem this function must accept strings instead of char*.有人告诉我要解决这个函数必须接受字符串而不是 char* 的另一个问题,我得到了帮助。 I thought I fixed all the errors related to replacing char* with string but I can't seem to fix this last one.我以为我修复了与用字符串替换 char* 相关的所有错误,但我似乎无法修复最后一个。 Please please help me!请帮帮我! Here is the problem function:这是问题函数:

List_Node *listTextEditor::create_node(string value)//creates the list elements
{
        struct List_Node *tempNode, *s;
        tempNode = new(struct List_Node);
        if (tempNode == NULL)
        {
                cout << "Memory not allocated " << endl;//if theres nothing in the list
                return 0;
        }
        else
        {
                tempNode->textLine=value ; //This puts stuff in the current node and creates/moves to the next. THIS IS WHERE THE PROBLEM IS!!!!!!!!!
                tempNode->nextEle = NULL;
                return tempNode;
        }
}

From the error message I assume, that your List_Node class is defined somewhat like this:根据我假设的错误消息,您的List_Node类的定义有点像这样:

struct List_Node {
    char* textLine;
    List_Node* nextEle;
};

You cannot assign a std::string to a char* (the latter is a C-style string, that requires manual memory management).您不能将std::string分配给char* (后者是 C 风格的字符串,需要手动内存管理)。 Since you are using C++, stick to it's string class std::string .由于您使用的是 C++,因此请坚持使用它的字符串类std::string

Change your class definition to this instead:改为将您的类定义更改为:

struct List_Node {
    std::string textLine;
    List_Node* nextEle;
};


There are other issues with your code, not immediately related to your error. 您的代码还有其他问题,与您的错误没有直接关系。 Once you convert it to a reasonable implementation, it's hardly even worth a function call anymore: 一旦你把它转换成一个合理的实现,它就不再值得一个函数调用了:

 List_Node *listTextEditor::create_node(string value) { return new ListNode{value, nullptr}; }

It would be helpful if you provided the definition of List_Node .如果您提供了List_Node的定义,那将会很有帮助。 I'll assume the following.我将假设以下内容。

struct List_Node {
    char *textLine;
    List_Node *nextEle;
};

Now, a char * type is just a pointer to some char data.现在, char *类型只是指向某些char数据的指针。 It doesn't actually allocate any memory for that data to reside in. You can't assign a std::string value to a char * variable, because unless you allocate memory, that char * doesn't have anywhere to store a string.它实际上并没有为该数据分配任何内存。您不能将std::string值分配给char *变量,因为除非您分配内存,否则char *没有任何地方可以存储字符串. (And then even if you had allocated enough memory to hold the string, you'd still need to do a string copy rather than a plain assignment, because you want to copy the underlying string data, not just change the pointer address.) That means you either need to allocate the memory yourself, and delete it when you are finished with it, or use a type like std::string which does its own memory allocation internally. (然后即使您分配了足够的内存来保存字符串,您仍然需要进行字符串复制而不是简单的赋值,因为您想要复制底层字符串数据,而不仅仅是更改指针地址。)意味着您要么需要自己分配内存,并在完成后将其删除,要么使用像std::string这样的类型,它在内部进行自己的内存分配。

For the former, you'd do something like this, and would be obligated to delete[] textLine when deleting list nodes .对于前者,你会做这样的事情,并且在删除列表节点时有义务delete[] textLine

{
    tempNode->textLine = new char[value.length()+1];
    strcpy(tempNode->textLine, value.c_str());
    tempNode->nextEle = NULL;
    return tempNode;
}

For the latter, you'd just change the definition of List_Node .对于后者,您只需更改List_Node的定义。

struct List_Node {
    std::string textLine;
    List_Node *nextEle;
};

An unrelated issue is that new does not return NULL when it cannot allocate memory.一个不相关的问题是new在无法分配内存时不会返回NULL It throws a bad_alloc exception.它抛出一个bad_alloc异常。 So if you want to check whether the allocation succeeded, you'd actually need to either put it in a try-catch block, or use new (std::nothrow) List_Node to instruct it to return NULL on failure rather than throw an exception.因此,如果您想检查分配是否成功,您实际上需要将它放在 try-catch 块中,或者使用new (std::nothrow) List_Node指示它在失败时返回 NULL 而不是抛出异常. Or you can just ignore failures and allow the case of a failed memory allocation to cause an unhandled exception and terminate program execution, since you're probably not going to be able to recover from the system running out of memory anyway, and given the simplicity of your program, are only likely to encounter this problem if you've got an infinite loop that is continuously allocating memory.或者您可以忽略故障并允许内存分配失败的情况导致未处理的异常并终止程序执行,因为无论如何您可能无法从内存不足的系统中恢复,并且考虑到简单性如果您有一个不断分配内存的无限循环,则只有您的程序才有可能遇到此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char> - Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment 错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;| - error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'| 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM