我在我的程序中使用了以下结构。C++发生结构错误,为什么不允许这样做?
struct terminator{
int id;
string type;
union{
terminator *next;
int empty;
};
};
在主,我有以下代码:
int main(){
terminator root = {0, "", NULL};
root = {0, "", NULL}; //NOT ALLOWED WHY? Trying to set to its original value.
}
这提供了以下错误信息:
g++ lab8.cc -std=c++11
lab8.cc: In function 'int main()':
lab8.cc:78:21: error: no match for 'operator=' in 'root = {0, "", 0}'
lab8.cc:78:21: note: candidates are:
lab8.cc:6:8: note: terminator& terminator::operator=(const terminator&)
lab8.cc:6:8: note: no known conversion for argument 1 from '<brace-enclosed in
itializer list>' to 'const terminator&'
lab8.cc:6:8: note: terminator& terminator::operator=(terminator&&)
lab8.cc:6:8: note: no known conversion for argument 1 from '<brace-enclosed in
itializer list>' to 'terminator&&'
但是,这是确定的,而不是:
int main(){
terminator root = {0, "", NULL};
root = *(new terminator);
root.id=0;
root.type="";
root.next=NULL;
}
为什么这是吗?任何方式来解决它?
与该问题没有直接关系,但在C++ 11中,'NULL'的使用已被弃用,以支持'nullptr'。 – Gorpik 2013-03-19 09:09:39
@Gorpik这很有趣。谢谢(你的)信息! – 2013-03-19 09:10:10