プログラムで次の構造体を使用しています。
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;
}
なんでそうなの?それを回避する方法はありますか?