943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2080
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 21st, 2008
0

Re: Object compiler errors

one die header:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Mar 21st, 2008
1

Re: Object compiler errors

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
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 21st, 2008
0

Re: Object compiler errors

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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Mar 21st, 2008
0

Re: Object compiler errors

I got it to compile without error. Problem seems to be here
C++ Syntax (Toggle Plain Text)
  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
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 21st, 2008
0

Re: Object compiler errors

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?
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Mar 21st, 2008
0

Re: Object compiler errors

Okay, the switch isn't working, I'm getting a bunch of illegal cases. Do I have the wrong condition?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Mar 21st, 2008
1

Re: Object compiler errors

Either you didn't know that you should use braces, or you forgot.

cpp Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 21st, 2008
0

Re: Object compiler errors

okay, I feel stupid now
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Mar 21st, 2008
0

Re: Object compiler errors

you don't need that switch statement at all
C++ Syntax (Toggle Plain Text)
  1. for (int i=0;i<rollsize;i++)
  2. {
  3. allresults[i] = allresults [i]+1;
  4. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 22nd, 2008
0

Re: Object compiler errors

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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. void displayResults(int allresults[])
  2. {
  3. cout << allresults << endl;
  4. }
Last edited by henpecked1; Mar 22nd, 2008 at 12:23 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: confusion in virtual
Next Thread in C++ Forum Timeline: math operations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC