我在我的程序中使用了以下結構。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