structure required?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    #1

    structure required?

    i am working with a vector of pointers to Job class objects(job class is one that i defined)
    when a person in my game dies, a pointer to the job he once had is added to a vector to track jobs that have no labor.

    anyway when i try to use these they dont work

    this is what i tried:

    Code:
      for(int x =0;x<emptyJobs.size();x++)
      {
       string s = *emptyJobs[x].getName();
       if(unemp.size()==0)
        addNews("a(n) "+s+" has no labor.");
      }
    for some reason i get this from the compiler:
    Code:
    [C++ Error] Keep.cpp(57): E2294 Structure required on left side of . or .*
    any suggestions as to what i am doing wrong??


    thanks alot
    ken
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by drsmooth
    i am working with a vector of pointers to Job class objects(job class is one that i defined)
    when a person in my game dies, a pointer to the job he once had is added to a vector to track jobs that have no labor.

    anyway when i try to use these they dont work

    this is what i tried:

    Code:
      for(int x =0;x<emptyJobs.size();x++)
      {
       string s = *emptyJobs[x].getName();
       if(unemp.size()==0)
        addNews("a(n) "+s+" has no labor.");
      }
    for some reason i get this from the compiler:
    Code:
    [C++ Error] Keep.cpp(57): E2294 Structure required on left side of . or .*
    any suggestions as to what i am doing wrong??


    thanks alot
    ken
    The dot operator has higher precedence than the dereference operator.
    So you need parentheses:
    Code:
       string s = (*emptyJobs[x]).getName();
    This is exactly why C++ has the indirection operator, ->.
    You can rewrite the code as:
    Code:
       string s = emptyJobs[x]->getName();

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      now i fixed the code to both of the two ways and got the same exception:

      this time at runtime:
      ---------------------------
      Debugger Exception Notification
      ---------------------------
      Project Project1.exe raised exception class std::bad_alloc with message 'Exception Object Address: 0xF5BA42'. Process stopped. Use Step or Run to continue.
      ---------------------------
      OK Help
      ---------------------------

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Apparently you are using the new operator without try/catch blocks.

        A bad_alloc usually means you have run out of heap memory.

        A try/catch should allow you to see where the exception is being thrown.

        Comment

        • drsmooth
          New Member
          • Oct 2007
          • 112

          #5
          what would the catch bloc kcatch? i am familiar with try/catch from java but i dont know how the exceotions are written...

          thanks,
          ken

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            C++ STL objects use a hierarchy for exceptions. The base class is exception. So you could catch that:

            [code=cpp]
            int* ptr = 0;
            try
            {
            ptr = new int[100000];
            }
            catch( exception& err)
            {
            cout << err.what() << endl; //see what the error was
            return; //bail out of the function
            }
            [/code]

            The what() method returns a char* so you can use cout on it.

            bad_alloc derives from exception so you could catch that also. The difference is that if you catch a bad_alloc you kow what you caught. Whereas with an exception, you don't know that. All you know is that itis some kind of exception.

            You could have two catch blocks to differentiate:

            [code=cpp]
            int* ptr = 0;
            try
            {
            ptr = new int[100000];
            }
            catch(bad_alloc & err)
            {
            cout << err.what() << endl; //see what the bad_alloc error was
            return; //bail out of the function

            }
            catch( exception& err)
            {
            //not a bad_alloc
            cout << err.what() << endl; //see what the error was
            return; //bail out of the function
            }
            [/code]

            Comment

            • drsmooth
              New Member
              • Oct 2007
              • 112

              #7
              i found the line where the exception was being throw from but th catch statement doesnt seem to be working

              Code:
               //cycle through empty jobs and try to fill them
                for(int x =0;x<emptyJobs.size();x++)
                {
                 string s;
                 try{
                 s = (*emptyJobs[x]).getName();
                 }catch(exception& err){return;}
              
                 if(unemp.size()==0)
                  addNews("a(n) "+s+" has no labor.");
                 else
                 {
              //*********************************************************************
                 }
                }
               }
              thats what i have, when i step through the code the exception still causes the program to crash...?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                After your catch(exception &) block add a catch(...) block:
                [code=c]
                try
                {
                //some code
                }
                catch(exception & err)
                {
                //exception objects caught here
                }
                catch (...)
                {
                //anything else caught here
                }
                [/code]

                If neither catch block catches the exception, then what you are calling an exception isn't an exception.

                Let me know what you find.

                Comment

                • drsmooth
                  New Member
                  • Oct 2007
                  • 112

                  #9
                  ---------------------------
                  Debugger Exception Notification
                  ---------------------------
                  Project Project1.exe raised exception class std::bad_alloc with message 'Exception Object Address: 0xF5BA42'. Process stopped. Use Step or Run to continue.
                  ---------------------------
                  OK Help
                  ---------------------------

                  this happens when i run this:

                  Code:
                   //cycle through empty jobs and try to fill them
                    try{
                    for(int x =0;x<emptyJobs.size();x++)
                    {
                     string s;
                     s = (*emptyJobs[x]).getName();
                  
                     if(unemp.size()==0)
                      addNews("a(n) "+s+" has no labor.");
                     else
                     {
                  //*********************************************************************
                     }
                    }
                    }catch(exception& err){return;}
                    catch(...){return;}

                  ..on a side note how do you get the code to be cpp instead of jus regualr code in the [CODE} tags?


                  thanks

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by drsmooth
                    on a side note how do you get the code to be cpp instead of jus regualr code in the [CODE} tags?
                    Use code=cpp between the brackets of the tag.

                    Have you placed a breakpoint inside this try block to see of you are getting there. That bad_alloc could be somewhere else in the program.

                    If not, then do so and run the code using your debugger.

                    Comment

                    • drsmooth
                      New Member
                      • Oct 2007
                      • 112

                      #11
                      well... i stepped through the code line by line from a break right before the try statement i showed above. i got to this line where the exception appears to happen:

                      [CODE=cpp] s = (*emptyJobs[x]).getName();[/CODE]

                      then i stepped through a gain and used 'evaluate/modify' to check the status of (*emptyJobs[x]).getName().

                      thats where it returned this:

                      E2122 Function call terminated by unhandled exception 0xeefface at address 0x7c812a5b

                      i checked what (*emptyJobs[x]) was and got this:

                      { true, 1580, 1246764, 0, { :00000001, :0012FA40, { :00000111 } }, { :0013062C, :0012FA5C, { NULL } }, 20575260, 261, 1243872, 2118282740, { "Ðk", "", { ",\x06\x13" } } }

                      i suppose that represents values stored in the class object?

                      my question now is what does:
                      E2122 Function call terminated by unhandled exception 0xeefface at address 0x7c812a5b
                      mean?

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        And emptyJobs is a non-null pointer?? Right.

                        You can't dereference a null pointer.

                        If emptyJobs is OK as a pointer, then you may have to give me a bigger peek at your code. Expecially, the class declaration and the definition of the getName() method.

                        Comment

                        • drsmooth
                          New Member
                          • Oct 2007
                          • 112

                          #13
                          the emptyJobs is pointer array which looks like this right before the error:

                          { :00EAF718, :00EAF71C, { :00EAF71C } }

                          the get name function is defined as:

                          [CODE=cpp]string Job::getName(){ return jobName;}[/CODE]

                          jobName is defined here:
                          [CODE=cpp]
                          Job::Job(string x, int initialOffset)
                          {
                          startDelay = initialOffset;
                          dataFile = "data\\jobs\\"+ x+".dat";
                          analyzeDataFile ();
                          hasWorker=true;
                          }[/CODE]

                          if this is still not enough let me know. tonight i am going to try and drastically simplify the process here...i ended up with some superflous classes that i am going to try and weed out and maybe that will make this easier to correct...

                          thanks for all your help

                          Comment

                          Working...