【发布时间】:2016-11-30 12:55:45
【问题描述】:
我创建了一个名为Employee 的类,私下我有一个Name 作为string。这是我的班级声明:
class Employee
{
string Name;
public:
Employee();
void SetName(string);
void StringToEmployee(string);
~Employee();
}
这是StringToEmployee(string)方法的定义:
void Employee::StringToEmployee(string s)
{
char *first = s, *end = s+strlen(s), *last = NULL;
last = find(first, end, ',');
string temp(first, last- first);
SetName(temp);
}
当我调试到string temp(first, last- first) 行时发生错误,编译器似乎不允许我在方法中构造新字符串。因为我也变成了string temp;,然后是temp.assign(first, last-first)。错误仍然存在。如何在方法中创建新字符串?
【问题讨论】:
-
std::strlen函数是一个 C 函数。它需要一个 C 以 null 结尾的字节字符串作为输入。 IE。指向char的指针。尝试改用std::stringfunctions。 -
你到底把
string定义为什么? MCVE 的其余部分在哪里?而且,什么是错误的?