我在我的代码以下现有类:分配一个std :: basic_string的一个字符串
struct Aclass {
typedef std::string TitleType;
TitleType title;
typedef std::size_t NumType;
NumType some_num;
};
的Aclass
实例化后,让我们说aclass
,我设置aclass.title
在我的节目一些字符串。在不同的功能,我要做到以下几点:
NumType new_num;
std::string new_string;
new_num = aclass.some_num; // this works, verified by print statement
new_string = aclass.title; // this doesn't work
typeid(aclass.title)
给人basic_string
类型预期,但分配给new_string
导致我的程序崩溃。我怎样才能正确地完成这项任务?是否需要转换basic_string
到string
?
新是一个保留字 而A是一个类,而不是一个对象。 – CashCow 2014-09-30 14:15:56
这就是如何做到这一点,只要这两个都是有效的对象。 'new_string'是有效的,因为你刚创建它;但我们对“A”一无所知。你是怎么创造它的,你确定它从那时起一直没有被破坏?你能发布一个完整的,可编辑的例子来演示崩溃吗? – 2014-09-30 14:18:50
我知道'A.title'的内容是有效的。我可以打印'std :: string(A.title)'来验证。 – squareskittles 2014-09-30 14:51:39