473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C++ Need help with array of objects

21
Hello!

I am a little stuck, I need to create a array of objects (class) and seem to having some troubles.

I will only post up my main file hopefully it will be enough.

You can see I have an array of object plant, when I compile with the header file and cpp, I get "left of '.plantName' must have class/struct/union type" errors on all my variables. I think I have a problem with my constructor maybe..

thankyou in advance.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "nursery.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int selection = 0; 
  10.     int i = 0;
  11.  
  12.     Nursery plant[10];
  13.     plant[i].menu();
  14.     cin >> selection;
  15.  
  16.     while (selection != 4)
  17.     {
  18.         switch (selection)
  19.         {
  20.         case 1:        plant[i].addPlant();
  21.             break;
  22.         case 2:        plant[i].waterPlant();
  23.             break;
  24.         case 3:        plant[i].movePlant(); 
  25.             break;
  26.         case 4:    // exit
  27.             break;
  28.  
  29.         default:    cout << "Illegal menu selection" << endl;
  30.         }
  31.  
  32.     };
  33.  
  34.     system ("pause");
  35. }
Sep 16 '07 #1
13 1660
Hackles
18
Hello!

I am a little stuck, I need to create a array of objects (class) and seem to having some troubles.

I will only post up my main file hopefully it will be enough.

You can see I have an array of object plant, when I compile with the header file and cpp, I get "left of '.plantName' must have class/struct/union type" errors on all my variables. I think I have a problem with my constructor maybe..

thankyou in advance.



#include <iostream>
#include <string>
#include "nursery.h"

using namespace std;

int main()
{
int selection = 0;
int i = 0;

Nursery plant[10];
plant[i].menu();
cin >> selection;

while (selection != 4)
{
switch (selection)
{
case 1: plant[i].addPlant();
break;
case 2: plant[i].waterPlant();
break;
case 3: plant[i].movePlant();
break;
case 4: // exit
break;

default: cout << "Illegal menu selection" << endl;
}

};

system ("pause");
}
First of all, please use the code tags when posting source code by using by using the CODE tags to enclose source code.
Now, there are some obvious errors in your source code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include "nursery.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int selection = 0; 
  10.     int i = 0;
  11.  
  12.     Nursery plant[10];
  13.     plant[i].menu();
  14.     cin >> selection;
  15.  
  16.     while (selection != 4)
  17.     {
  18.         switch (selection)
  19.         {
  20.         case 1:        plant[i].addPlant();
  21.             break;
  22.         case 2:        plant[i].waterPlant();
  23.             break;
  24.         case 3:        plant[i].movePlant(); 
  25.             break;
  26.         case 4:    // exit
  27.             break;
  28.  
  29.         default:    cout << "Illegal menu selection" << endl;
  30.         }
  31.  
  32.     };
  33.  
  34.     system ("pause");
First of all, it seems that once you input a selection, you go into an infinite loop - as long as the selection is not 4, the condition for the while loop is met and the switch repeats indefinately.
Also, it seems that your code only ever references the first 'plant' object in the array (plant[0]).
And finally, your while loop has already tested for whether selection equals 4, so you wouldn't need to include case 4 in your switch statement.
Other than that, nothing appears to be out of the ordinary, and the chances are the error is in the Nursery class prototypes or definitions. If you provide a complete listing, or perhaps an attachment, I could help you better.
Have fun!
Sep 16 '07 #2
Micko1
21
thankyou for the reply, I really appreciate it!

the switch I was basically writing off the top of my head at the time, and I have since corrected it.

I could attach the other files, however I am unsure of how to do this on this forum.

Due to the forum guidelines I don't want to just post up all the code(copy and paste)

What should I do, just post up all the required code?

Thanks in advance.

EDIT: I have worked out how to attach!!
Sep 16 '07 #3
ilikepython
844 Expert 512MB
thankyou for the reply, I really appreciate it!

the switch I was basically writing off the top of my head at the time, and I have since corrected it.

I could attach the other files, however I am unsure of how to do this on this forum.

Due to the forum guidelines I don't want to just post up all the code(copy and paste)

What should I do, just post up all the required code?

Thanks in advance.

EDIT: I have worked out how to attach!!
I tried your code and there were a lot of 'plant' not defined errors. Once I fixed those it worked. There was no left of '.plant' must be class error.
Sep 16 '07 #4
Hackles
18
thankyou for the reply, I really appreciate it!

the switch I was basically writing off the top of my head at the time, and I have since corrected it.

I could attach the other files, however I am unsure of how to do this on this forum.

Due to the forum guidelines I don't want to just post up all the code(copy and paste)

What should I do, just post up all the required code?

Thanks in advance.

EDIT: I have worked out how to attach!!
I'm sorry, but I think you may have rationalized your classes incorrectly. Judging by your program, you have a class that stores plant data but you have put functions to manage a nursery withing the same class - this can be accommodated for with static members or by passing the array through to the function, but that is highly inefficient. I've tried to rationalize it in a better way, by separating plants and nurseries - hope this helps.
Have fun!
Sep 16 '07 #5
ilikepython
844 Expert 512MB
I'm sorry, but I think you may have rationalized your classes incorrectly. Judging by your program, you have a class that stores plant data but you have put functions to manage a nursery withing the same class - this can be accommodated for with static members or by passing the array through to the function, but that is highly inefficient. I've tried to rationalize it in a better way, by separating plants and nurseries - hope this helps.
Have fun!
I agrree, your nursery class acted kind of like a plant class. It might be a good idea to write a plant class and have the nursery class as a container for plant objects.
Sep 16 '07 #6
Hackles
18
I agrree, your nursery class acted kind of like a plant class. It might be a good idea to write a plant class and have the nursery class as a container for plant objects.
ilikepython, that's what I did :P (refer to attachment on post #6).
Have fun!
Sep 16 '07 #7
Micko1
21
I'm sorry, but I think you may have rationalized your classes incorrectly. Judging by your program, you have a class that stores plant data but you have put functions to manage a nursery withing the same class - this can be accommodated for with static members or by passing the array through to the function, but that is highly inefficient. I've tried to rationalize it in a better way, by separating plants and nurseries - hope this helps.
Have fun!
I thankyou very much for the time you have taken to help me out!

this is very close to the end product I was trying to achieve, apart from a few minor details.

thankyou very much!
Sep 16 '07 #8
Micko1
21
one question, depending on the location of the plants (inside or outside) determines how much sunlight level/hydration is gained or lost per turn (loops of menu)

I seem to be having trouble with this... i can add a to a single plants sunlight and hydration no problem, but how to effect all plants each turn, depending on their location?

start with 35 hydro/40 sun

watering = +50 hydro

per turn if indoor -15 hydro -10 sun
if outdoor +40 sun -30 water

warning if hydro and sun reach less than 10 or greater than 90, both values cannot go below 0 or greater than 100 (just reset to 0 or 100 respectivley)

Cheers!

which also brings me to this... is these are set values, should I have them as private members of the class?
Sep 16 '07 #9
sicarie
4,677 Expert Mod 4TB
Hey guys. I just wanted to let you know that I've removed the zip file attachments - due to the nature of zip files, we have to remove them when they're posted. If you guys wouldn't mind posting further code examples in code tags, I'd appreciate it.

Thanks,

sicarie
Sep 16 '07 #10
Micko1
21
Hey guys. I just wanted to let you know that I've removed the zip file attachments - due to the nature of zip files, we have to remove them when they're posted. If you guys wouldn't mind posting further code examples in code tags, I'd appreciate it.

Thanks,

sicarie

Not a problem, understand 100%

just to let the kind people that helped out, I solved the problem with a basic turn function.

Thanks again
Sep 16 '07 #11
ilikepython
844 Expert 512MB
ilikepython, that's what I did :P (refer to attachment on post #6).
Have fun!
Oops, didn't check your attached file, sorry about that. ;-)
Sep 16 '07 #12
Hackles
18
Hey guys. I just wanted to let you know that I've removed the zip file attachments - due to the nature of zip files, we have to remove them when they're posted. If you guys wouldn't mind posting further code examples in code tags, I'd appreciate it.

Thanks,

sicarie
I understand sicarie; however, for particularly long code listings, should I attach each source file individually?

Thanks for the pointer (0xAF42389C - sorry, bad pun :P ) and have fun!
Sep 17 '07 #13
sicarie
4,677 Expert Mod 4TB
I understand sicarie; however, for particularly long code listings, should I attach each source file individually?

Thanks for the pointer (0xAF42389C - sorry, bad pun :P ) and have fun!
Very bad pun ;). It's preferred that you only post the relevant part, though for some questions it is necessary to post full code, as I don't think the bb software here will accept any of the .c/.cpp extensions you'll probably have to move them to a .txt or something. But then, it's really up to you, if you want to put it in a single source file or if you want to attach the different files you are referencing, you'll probably want to let whoever is helping you know. In general, it would probably be easier for the Experts here if they were separate, but it is up to you.

Thanks again!
Sep 17 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.