Object compiler errors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #11
Mar 21st, 2008
one die header:
  1. #ifndef ONEDIE_H
  2. #define ONEDIE_H
  3.  
  4. class onedie
  5. {
  6. public:
  7. void rollit();
  8. int getroll();
  9.  
  10. private:
  11. int result;
  12. };
  13.  
  14. #endif

threedice header:
  1. #ifndef THREEDICE_H
  2. #define THREEDICE_H
  3.  
  4. class threedice
  5. {
  6. public:
  7. void rollem();
  8. int getsumFaces();
  9.  
  10. private:
  11. onedie die1;
  12. onedie die2;
  13. onedie die3;
  14. };
  15.  
  16. #endif

noclass header:
  1. #ifndef NOCLASS_H
  2. #define NOCLASS_H
  3.  
  4. void statistics (int rolls[],int rollsize, int allresults[]); //prototype given by instructor
  5.  
  6. //display function here
  7.  
  8. #endif

onedie.cpp
  1. #include "onedie.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void onedie::rollit()
  10. {
  11. srand(time_t(NULL));
  12. result= 1 + rand() % 6;
  13. }
  14.  
  15. int onedie::getroll()
  16. {
  17. return result;
  18. }

threedice.cpp
  1. #include "threedice.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void threedice::rollem()
  7. {
  8. die1.rollit();
  9. die2.rollit();
  10. die3.rollit();
  11. }
  12.  
  13. int threedice::getsumFaces()
  14. {
  15. return die1.getroll()+die2.getroll()+die3.getroll();
  16. }

noclass.cpp
  1. #include "noclass.h"
  2. #include "threedice.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void statistics (int rolls[], int rollsize,int allresults[])
  8. {
  9. threedice tryerluck;
  10. rollsize=200;
  11. rolls[rollsize];
  12. allresults[16];
  13.  
  14. for(int i=0;i<200;i++)
  15. {
  16. tryerluck.rollem();
  17. rolls[i]= tryerluck.getsumFaces();
  18. }
  19. }

Okay, that's all of it. Where did I screw up please?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Object compiler errors

 
1
  #12
Mar 21st, 2008
In your THREEDICE.H add the following forward declaration
#define THREEDICE_H
class onedie;
class threedice
...

Then the statistics() function is a bit odd, if you think it will be called from e.g. main(),
it probably should look like
  1. int main()
  2. {
  3. const int roll_count = 200;
  4. int rolls[roll_count] = {0};
  5. // I'm not actually sure what the allresults should be
  6. int allresults[roll_count] = {0};
  7. // Then you can make the statistics() call ...
  8. statistics (rolls, roll_count, allresults);
  9. return 0;
  10. }
  11.  
  12. void statistics (int rolls[], int count, int allresults[])
  13. {
  14. threedice tryerluck;
  15. for(int i=0;i<count;i++)
  16. {
  17. tryerluck.rollem();
  18. rolls[i]= tryerluck.getsumFaces();
  19. }
  20.  
  21. // and maybe do something here with the allresults too
  22. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #13
Mar 21st, 2008
I put the class onedie; in the threedice header and it says undefined class...did I leave something out or have I incorrectly defined it in onedie.cpp?

I was indeed going to call the statistics function in main and there will be more to it. After the for loop, I have to make another one (I think) with a switch to fill the allresults with the spread of results between 3 and 18.

update:
I changed it from class onedie; to #include onedie.h and now it compiles, is that correct?
Last edited by henpecked1; Mar 21st, 2008 at 10:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Object compiler errors

 
0
  #14
Mar 21st, 2008
I got it to compile without error. Problem seems to be here
  1. #ifndef THREEDICE_H
  2. #define THREEDICE_H
  3.  
  4. #include "onedie.h" // <<<<<<<<<<<<<<<<<< ADD THIS LINE
  5.  
  6. class threedice
  7. {
  8. public:
  9. void rollem();
  10. int getsumFaces();
  11.  
  12. private:
  13. onedie die1;
  14. onedie die2;
  15. onedie die3;
  16. };
  17.  
  18. #endif
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #15
Mar 21st, 2008
that's what I did...ooo...I almost figured something out on my own...lol.

Okay, now I have to try the loop to fill allresults. I'm thinking a for loop with a switch, does that sound right....or at least like it would work?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #16
Mar 21st, 2008
Okay, the switch isn't working, I'm getting a bunch of illegal cases. Do I have the wrong condition?

  1.  
  2. for (int i=0;i<rollsize;i++)
  3. {
  4. switch (rolls[i])
  5. case 3:allresults[0] = allresults [0]+1; break;
  6. case 4:allresults[1] = allresults [1]+1; break;
  7. case 5:allresults[2] = allresults [2]+1; break;
  8. case 6:allresults[3] = allresults [3]+1; break;
  9. case 7:allresults[4] = allresults [4]+1; break;
  10. case 8:allresults[5] = allresults [5]+1; break;
  11. case 9:allresults[6] = allresults [6]+1; break;
  12. case 10:allresults[7] = allresults [7]+1; break;
  13. case 11:allresults[8] = allresults [8]+1; break;
  14. case 12:allresults[9] = allresults [9]+1; break;
  15. case 13:allresults[10] = allresults [10]+1; break;
  16. case 14:allresults[11] = allresults [11]+1; break;
  17. case 15:allresults[12] = allresults [12]+1; break;
  18. case 16:allresults[13] = allresults [13]+1; break;
  19. case 17:allresults[14] = allresults [14]+1; break;
  20. case 18:allresults[15] = allresults [15]+1; break;
  21. }
Last edited by WolfPack; Mar 21st, 2008 at 11:45 pm. Reason: Corrected indenting.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Object compiler errors

 
1
  #17
Mar 21st, 2008
Either you didn't know that you should use braces, or you forgot.

  1. for (int i=0;i<rollsize;i++)
  2. {
  3. switch (rolls[i])
  4. { // Forgot this
  5. case 3:allresults[0] = allresults [0]+1; break;
  6. case 4:allresults[1] = allresults [1]+1; break;
  7. /* ......... */
  8. case 17:allresults[14] = allresults [14]+1; break;
  9. case 18:allresults[15] = allresults [15]+1; break;
  10. } // And this
  11. }
Last edited by WolfPack; Mar 21st, 2008 at 11:50 pm. Reason: Corrected indenting.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #18
Mar 21st, 2008
okay, I feel stupid now
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Object compiler errors

 
0
  #19
Mar 21st, 2008
you don't need that switch statement at all
  1. for (int i=0;i<rollsize;i++)
  2. {
  3. allresults[i] = allresults [i]+1;
  4. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Object compiler errors

 
0
  #20
Mar 22nd, 2008
AD, does that increment the different elements of the results array so you know which numbers are rolled how many times? I used the switch because that's what the prof has in the lesson plan, but I'm just as keen on keeping things simple.

Okay, main looks like it's outputting a memory address instead of a bunch of dice rolls. Should I just move that statistics and display functions to main instead of calling them? Or I might have screwed up the display function....it's supposed to print each element of the allresults array.

Here is main:
  1. #include "threedice.h"
  2. #include "noclass.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. const int rollsize = 200;
  10. int rolls[rollsize] = {0};
  11. int allresults[16]={0};
  12.  
  13.  
  14. statistics(rolls,rollsize,allresults);
  15. displayResults(allresults);
  16.  
  17.  
  18. return 0;
  19. }

The display function
  1. void displayResults(int allresults[])
  2. {
  3. cout << allresults << endl;
  4. }
Last edited by henpecked1; Mar 22nd, 2008 at 12:23 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC