#include<iostream> using namespace std; struct fruit{ char name[50]; double weight, volume, calories; bool poisonous; }; struct container{ fruit * fruits; double weight, volume; }; int main(){ int some_number_determined_later_in_code; int another_number_determined_later_in_code; container * containers = new(nothrow) containers [some_number_determined_later_in_code]; containters.fruits = new (nothrow) fruit[int another_number_determined_later_in_code;]; cout<<"Thanks for solving the problem"; system ("pause"); return 0; } |
std::vector
instead of using dynamic arrays.
prog.cpp: In function ‘int main()’: prog.cpp:21: error: expected type-specifier before ‘containers’ prog.cpp:21: error: cannot convert ‘int*’ to ‘container*’ in initialization prog.cpp:21: error: expected ‘,’ or ‘;’ before ‘containers’ prog.cpp:22: error: ‘containters’ was not declared in this scope prog.cpp:21: warning: unused variable ‘containers’ |
#include<iostream> using namespace std; struct fruit{ char name[50]; double weight, volume, calories; bool poisonous; }; struct container{ fruit * fruits; double weight, volume; }; int main(){ int some_number_determined_later_in_code; int another_number_determined_later_in_code; some_number_determined_later_in_code= 3; //just added this to get rid of an error. another_number_determined_later_in_code=2; //just added this to get rid of an error. container * containers = new(nothrow) containers [some_number_determined_later_in_code]; containters.fruits = new (nothrow) fruit[another_number_determined_later_in_code]; cout<<"Thanks for solving the problem"; //system ("pause"); return 0; } |
new containers
should be new container
containters.fruits
should be containters->fruits
#include<iostream> using namespace std; struct fruit{ char name[50]; double weight, volume, calories; bool poisonous; }; struct container{ fruit * fruits; double weight, volume; }; int main(){ int some_number_determined_later_in_code; int another_number_determined_later_in_code; some_number_determined_later_in_code= 3; //just added this to get rid of an error. another_number_determined_later_in_code=2; //just added this to get rid of an error. container * containers = new(nothrow) container [some_number_determined_later_in_code]; containters->fruits = new (nothrow) fruit[another_number_determined_later_in_code]; cout<<"Thanks for solving the problem"; //system ("pause"); return 0; } |
prog.cpp: In function ‘int main()’: prog.cpp:22: error: ‘containters’ was not declared in this scope prog.cpp:21: warning: unused variable ‘containers’ |
|
|
int main(){ int some_number_determined_later_in_code; int another_number_determined_later_in_code; some_number_determined_later_in_code= 3; //just added this to get rid of an error. another_number_determined_later_in_code=2; //just added this to get rid of an error. container * containers = new(nothrow) container [some_number_determined_later_in_code]; for(int i = 0; i< some_number_determined_later_in_code; ++i){ containers[i].fruits = new (nothrow) fruit[another_number_determined_later_in_code]; } cout<<"Finally Done!"; system ("pause"); return 0; } |