C++ Need help with array of objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Micko1
    New Member
    • Sep 2007
    • 21

    #1

    C++ Need help with array of objects

    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.


    [code=cpp]
    #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");
    }[/code]
    Last edited by sicarie; Sep 17 '07, 12:33 PM. Reason: Code tags
  • Hackles
    New Member
    • Sep 2007
    • 18

    #2
    Originally posted by Micko1
    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:
    [CODE=CPP]
    #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");
    } [/CODE]
    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!
    Last edited by Hackles; Sep 16 '07, 06:40 AM. Reason: Minor adjustments

    Comment

    • Micko1
      New Member
      • Sep 2007
      • 21

      #3
      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!!

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by Micko1
        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.

        Comment

        • Hackles
          New Member
          • Sep 2007
          • 18

          #5
          Originally posted by Micko1
          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!

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Hackles
            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.

            Comment

            • Hackles
              New Member
              • Sep 2007
              • 18

              #7
              Originally posted by ilikepython
              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!

              Comment

              • Micko1
                New Member
                • Sep 2007
                • 21

                #8
                Originally posted by Hackles
                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!

                Comment

                • Micko1
                  New Member
                  • Sep 2007
                  • 21

                  #9
                  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?

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    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

                    Comment

                    • Micko1
                      New Member
                      • Sep 2007
                      • 21

                      #11
                      Originally posted by sicarie
                      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

                      Comment

                      • ilikepython
                        Recognized Expert Contributor
                        • Feb 2007
                        • 844

                        #12
                        Originally posted by Hackles
                        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. ;-)

                        Comment

                        • Hackles
                          New Member
                          • Sep 2007
                          • 18

                          #13
                          Originally posted by sicarie
                          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!

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by Hackles
                            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!

                            Comment

                            Working...