Mar 20, 2013 at 12:19pm UTC
Hello, i was wondering if anyone was interested in helping me with my problem.The assignment is to create an array of objects from class called Objects.This is how the code that i have looks like:
(The classes are in its separate header file and i know i am supposed to include #include"nameofthefile.h" in all the files so i could use them properly)
class Object{
public:
string name;
double price;
Object();
Object(string name, double price);
};
class Store{
public:
Object* obj;
string address;
int nmrobject=0;
Store(string adr){
address=adr;
obj=new Object[10];
}
void addObject(Object o)
{
obj[nmrobject++] = o;//I don' know what to put in this function, and the
//assignment says i am not allowed to change nothing
//but how the class Store is written.I also need to
//print them out after they are created
}
void printOutObjects(){
}
};
int main(){
Store d("Some address");
d.addObject(Object("bread", 7));
d.addObject(Object("milk", 6.5));
d.addObject(Object("soup", 3.25));
d.printOutObjects();();
return 0;
}
This is my first post on this forum, so thanks to everyone who can helpme on this even a little bit.
Last edited on Mar 20, 2013 at 12:29pm UTC
Mar 20, 2013 at 12:36pm UTC
1 2 3 4 5
void printOutObjects(){
for (int i = 0; i < nmrobjects; ++i) {
std:cout << obj[i].name << ' ' << obj[i].price << std::endl;
}
}
Did you want something like that?
Output:
bread 7
milk 6.5
soup 3.25
Last edited on Mar 20, 2013 at 12:36pm UTC
Mar 20, 2013 at 12:39pm UTC
Well, yeah something like that, but the main problem i am having is that i can't even create the objects that i am supposed to print out.
Still, thanks for a quick response. :D
Mar 20, 2013 at 12:43pm UTC
Wait. Did it give you an compile error? If so, post it here.
Also in line: d.printOutObjects();();
Did you specify constructors of Object?
Last edited on Mar 20, 2013 at 12:43pm UTC
Mar 20, 2013 at 12:48pm UTC
This is the build message that i get when trying to run the program:
obj\Debug\main.o||In function `main':|
main.cpp|14|undefined reference to `Object::Object(std::string, double)'|
main.cpp|15|undefined reference to `Object::Object(std::string, double)'|
main.cpp|16|undefined reference to `Object::Object(std::string, double)'|
obj\Debug\main.o||In function `ZN5StoreC1ESs':|
Store.h|33|undefined reference to `Object::Object()'|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|
Mar 20, 2013 at 2:21pm UTC
That is good suggestion but as i said previously i am not allowed to change nothing but the class Store.Everything else needs to stay like it is.
Again, thanks for a quick response.
Last edited on Mar 20, 2013 at 2:24pm UTC
Mar 20, 2013 at 2:25pm UTC
Ok, dont change class, write this
after class:
1 2
Object::Object() {};
Object::Object(std::string _name, double _price):name(_name), price(_price) {};
You
must specify those constructors, either in class, or outside it.
Last edited on Mar 20, 2013 at 2:26pm UTC
Mar 20, 2013 at 3:01pm UTC
Thanks so much for your help, it did the trick.You just saved me a lot of trouble trying to find the problem.I am just starting to learn object oriented programming and it is nice to know that there is someone who will help you if you just ask.
One more time , thank you.