468,491 Members | 1,991 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

structure required?

112 100+
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:

Expand|Select|Wrap|Line Numbers
  1.   for(int x =0;x<emptyJobs.size();x++)
  2.   {
  3.    string s = *emptyJobs[x].getName();
  4.    if(unemp.size()==0)
  5.     addNews("a(n) "+s+" has no labor.");
  6.   }
for some reason i get this from the compiler:
Expand|Select|Wrap|Line Numbers
  1. [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
Nov 21 '07 #1
12 4270
scruggsy
147 100+
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:

Expand|Select|Wrap|Line Numbers
  1.   for(int x =0;x<emptyJobs.size();x++)
  2.   {
  3.    string s = *emptyJobs[x].getName();
  4.    if(unemp.size()==0)
  5.     addNews("a(n) "+s+" has no labor.");
  6.   }
for some reason i get this from the compiler:
Expand|Select|Wrap|Line Numbers
  1. [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:
Expand|Select|Wrap|Line Numbers
  1.    string s = (*emptyJobs[x]).getName();
This is exactly why C++ has the indirection operator, ->.
You can rewrite the code as:
Expand|Select|Wrap|Line Numbers
  1.    string s = emptyJobs[x]->getName();
Nov 21 '07 #2
drsmooth
112 100+
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
---------------------------
Nov 22 '07 #3
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 22 '07 #4
drsmooth
112 100+
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
Nov 22 '07 #5
weaknessforcats
9,207 Expert Mod 8TB
C++ STL objects use a hierarchy for exceptions. The base class is exception. So you could catch that:

Expand|Select|Wrap|Line Numbers
  1. int* ptr = 0;
  2. try
  3. {
  4.      ptr = new int[100000];
  5. }
  6. catch( exception& err)
  7. {
  8.      cout << err.what() << endl;           //see what the error was
  9.      return;                                        //bail out of the function
  10. }
  11.  
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:

Expand|Select|Wrap|Line Numbers
  1. int* ptr = 0;
  2. try
  3. {
  4.      ptr = new int[100000];
  5. }
  6. catch(bad_alloc& err)
  7. {
  8.      cout << err.what() << endl;           //see what the bad_alloc error was
  9.      return;                                        //bail out of the function
  10.  
  11. }
  12. catch( exception& err)
  13. {
  14.                                                        //not a bad_alloc
  15.      cout << err.what() << endl;           //see what the error was
  16.      return;                                        //bail out of the function
  17. }
  18.  
Nov 22 '07 #6
drsmooth
112 100+
i found the line where the exception was being throw from but th catch statement doesnt seem to be working

Expand|Select|Wrap|Line Numbers
  1.  //cycle through empty jobs and try to fill them
  2.   for(int x =0;x<emptyJobs.size();x++)
  3.   {
  4.    string s;
  5.    try{
  6.    s = (*emptyJobs[x]).getName();
  7.    }catch(exception& err){return;}
  8.  
  9.    if(unemp.size()==0)
  10.     addNews("a(n) "+s+" has no labor.");
  11.    else
  12.    {
  13. //*********************************************************************
  14.    }
  15.   }
  16.  }
thats what i have, when i step through the code the exception still causes the program to crash...?
Nov 23 '07 #7
weaknessforcats
9,207 Expert Mod 8TB
After your catch(exception&) block add a catch(...) block:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     //some code
  4. }
  5. catch(exception& err)
  6. {
  7.     //exception objects caught here
  8. }
  9. catch (...)
  10. {
  11.      //anything else caught here
  12. }
  13.  
If neither catch block catches the exception, then what you are calling an exception isn't an exception.

Let me know what you find.
Nov 24 '07 #8
drsmooth
112 100+
---------------------------
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:

Expand|Select|Wrap|Line Numbers
  1.  //cycle through empty jobs and try to fill them
  2.   try{
  3.   for(int x =0;x<emptyJobs.size();x++)
  4.   {
  5.    string s;
  6.    s = (*emptyJobs[x]).getName();
  7.  
  8.    if(unemp.size()==0)
  9.     addNews("a(n) "+s+" has no labor.");
  10.    else
  11.    {
  12. //*********************************************************************
  13.    }
  14.   }
  15.   }catch(exception& err){return;}
  16.   catch(...){return;}
  17.  

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


thanks
Nov 24 '07 #9
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 25 '07 #10
drsmooth
112 100+
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:

Expand|Select|Wrap|Line Numbers
  1.    s = (*emptyJobs[x]).getName();
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?
Nov 25 '07 #11
weaknessforcats
9,207 Expert Mod 8TB
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.
Nov 26 '07 #12
drsmooth
112 100+
the emptyJobs is pointer array which looks like this right before the error:

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

the get name function is defined as:

Expand|Select|Wrap|Line Numbers
  1. string Job::getName(){return jobName;}
jobName is defined here:
Expand|Select|Wrap|Line Numbers
  1. Job::Job(string x, int initialOffset)
  2. {
  3.   startDelay = initialOffset;
  4.   dataFile = "data\\jobs\\"+x+".dat";
  5.   analyzeDataFile();
  6.   hasWorker=true;
  7. }
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
Nov 26 '07 #13

Post your reply

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

Similar topics

15 posts views Thread by damian birchler | last post: by
3 posts views Thread by ~~~ .NET Ed ~~~ | last post: by
14 posts views Thread by Just Me | last post: by
3 posts views Thread by Bob Simoneau | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.