【发布时间】:2017-02-12 00:02:00
【问题描述】:
我无法让这个程序正确编译。它适用于一个单链表的程序。这个特殊的功能让我很讨厌没有将其中的内容转换为字符串,但我看不到它。我得到了某人的帮助,他告诉我要解决另一个问题,这个函数必须接受字符串而不是 char*。我以为我修复了与用字符串替换 char* 相关的所有错误,但我似乎无法修复最后一个错误。请帮帮我! 这是问题函数:
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;
}
}
【问题讨论】:
-
textLine是字符串类成员数据吗?
-
没有从
std::string到(可变)char*的隐式转换。另外,与您的问题无关:if (tempNode == NULL)永远无法评估为true。看看operator new 做了什么。 -
添加类接口示例
-
错误信息没有告诉你吗?您正在尝试将 std::string 转换为 char*,但您不能。
标签: c++ function linked-list char