我在我的程序中使用以下结构。
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;
}
为什么会这样?有什么办法绕过它?