简体   繁体   中英

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*. I thought I fixed all the errors related to replacing char* with string but I can't seem to fix this last one. 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:

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). Since you are using C++, stick to it's string class 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 . 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. 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. (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.

For the former, you'd do something like this, and would be obligated to delete[] textLine when deleting list nodes .

{
    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 .

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

An unrelated issue is that new does not return NULL when it cannot allocate memory. It throws a bad_alloc exception. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM