i'm in need of assignment c++ class. problem trying compile program got professor. every time try compile code following error
"error: non-aggregate type 'circle' cannot initialized initializer list
circle list[] ={ { 93, "yellow" },
same error follows second circle in array. can tell me need code compile?
#include <iostream> #include "circle.h" #include <string> using namespace std; int main() { circle list[] ={ { 93, "yellow" }, { 27, "red" } }; circle *p; p = list; cout<<(*p).getradius()<<endl; cout<<(*p).getcolor()<<endl; cout<<p->getradius()<<endl; cout<<p->getcolor()<<endl; p++; cout<<(*p).getradius()<<endl; cout<<(*p).getcolor()<<endl; cout<<p->getradius()<<endl; cout<<p->getcolor()<<endl; return 0; }//end main
what version of c++ using? before c++11, class @ least 1 constructor not constructed using aggregate list:
struct { std::string s; int n; }; struct b { std::string s; int n; // default constructor b() : s(), n() {} }; int main() { a = { "hi", 3 }; // aggregate class: no constructors provided b b; // fine, use default constructor provided. aa; // fine, default construct since compiler make default constructor class don't provide 1 for. b bb = { "hello", 4 }; // won't work. b no longer aggregate class because constructor provided. return 0; }
i daresay circle has constructor defined, , cannot constructor aggregate initialisation list pre c++11. try:
circle list[] = { circle(93, "yellow"), circle(16, "red") };