| | |
Object compiler errors
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
one die header:
threedice header:
noclass header:
onedie.cpp
threedice.cpp
noclass.cpp
Okay, that's all of it. Where did I screw up please?
C++ Syntax (Toggle Plain Text)
#ifndef ONEDIE_H #define ONEDIE_H class onedie { public: void rollit(); int getroll(); private: int result; }; #endif
threedice header:
C++ Syntax (Toggle Plain Text)
#ifndef THREEDICE_H #define THREEDICE_H class threedice { public: void rollem(); int getsumFaces(); private: onedie die1; onedie die2; onedie die3; }; #endif
noclass header:
C++ Syntax (Toggle Plain Text)
#ifndef NOCLASS_H #define NOCLASS_H void statistics (int rolls[],int rollsize, int allresults[]); //prototype given by instructor //display function here #endif
onedie.cpp
C++ Syntax (Toggle Plain Text)
#include "onedie.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void onedie::rollit() { srand(time_t(NULL)); result= 1 + rand() % 6; } int onedie::getroll() { return result; }
threedice.cpp
C++ Syntax (Toggle Plain Text)
#include "threedice.h" #include <iostream> using namespace std; void threedice::rollem() { die1.rollit(); die2.rollit(); die3.rollit(); } int threedice::getsumFaces() { return die1.getroll()+die2.getroll()+die3.getroll(); }
noclass.cpp
C++ Syntax (Toggle Plain Text)
#include "noclass.h" #include "threedice.h" #include <iostream> using namespace std; void statistics (int rolls[], int rollsize,int allresults[]) { threedice tryerluck; rollsize=200; rolls[rollsize]; allresults[16]; for(int i=0;i<200;i++) { tryerluck.rollem(); rolls[i]= tryerluck.getsumFaces(); } }
Okay, that's all of it. Where did I screw up please?
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
In your THREEDICE.H add the following forward declaration
Then the statistics() function is a bit odd, if you think it will be called from e.g. main(),
it probably should look like
#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)
int main() { const int roll_count = 200; int rolls[roll_count] = {0}; // I'm not actually sure what the allresults should be int allresults[roll_count] = {0}; // Then you can make the statistics() call ... statistics (rolls, roll_count, allresults); return 0; } void statistics (int rolls[], int count, int allresults[]) { threedice tryerluck; for(int i=0;i<count;i++) { tryerluck.rollem(); rolls[i]= tryerluck.getsumFaces(); } // and maybe do something here with the allresults too }
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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?
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.
I got it to compile without error. Problem seems to be here
C++ Syntax (Toggle Plain Text)
#ifndef THREEDICE_H #define THREEDICE_H #include "onedie.h" // <<<<<<<<<<<<<<<<<< ADD THIS LINE class threedice { public: void rollem(); int getsumFaces(); private: onedie die1; onedie die2; onedie die3; }; #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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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)
for (int i=0;i<rollsize;i++) { switch (rolls[i]) case 3:allresults[0] = allresults [0]+1; break; case 4:allresults[1] = allresults [1]+1; break; case 5:allresults[2] = allresults [2]+1; break; case 6:allresults[3] = allresults [3]+1; break; case 7:allresults[4] = allresults [4]+1; break; case 8:allresults[5] = allresults [5]+1; break; case 9:allresults[6] = allresults [6]+1; break; case 10:allresults[7] = allresults [7]+1; break; case 11:allresults[8] = allresults [8]+1; break; case 12:allresults[9] = allresults [9]+1; break; case 13:allresults[10] = allresults [10]+1; break; case 14:allresults[11] = allresults [11]+1; break; case 15:allresults[12] = allresults [12]+1; break; case 16:allresults[13] = allresults [13]+1; break; case 17:allresults[14] = allresults [14]+1; break; case 18:allresults[15] = allresults [15]+1; break; }
Last edited by WolfPack; Mar 21st, 2008 at 11:45 pm. Reason: Corrected indenting.
Either you didn't know that you should use braces, or you forgot.
cpp Syntax (Toggle Plain Text)
for (int i=0;i<rollsize;i++) { switch (rolls[i]) { // Forgot this case 3:allresults[0] = allresults [0]+1; break; case 4:allresults[1] = allresults [1]+1; break; /* ......... */ case 17:allresults[14] = allresults [14]+1; break; case 18:allresults[15] = allresults [15]+1; break; } // And this }
Last edited by WolfPack; Mar 21st, 2008 at 11:50 pm. Reason: Corrected indenting.
バルサミコ酢やっぱいらへんで
you don't need that switch statement at all
C++ Syntax (Toggle Plain Text)
for (int i=0;i<rollsize;i++) { allresults[i] = allresults [i]+1; }
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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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:
The display function
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)
#include "threedice.h" #include "noclass.h" #include <iostream> using namespace std; int main () { const int rollsize = 200; int rolls[rollsize] = {0}; int allresults[16]={0}; statistics(rolls,rollsize,allresults); displayResults(allresults); return 0; }
The display function
C++ Syntax (Toggle Plain Text)
void displayResults(int allresults[]) { cout << allresults << endl; }
Last edited by henpecked1; Mar 22nd, 2008 at 12:23 am.
![]() |
Similar Threads
- compiler errors wargame (Java)
- Stupid question(using class object) (C++)
- New object initialization error (C++)
- Linking errors while creating dll using ATL project (C++)
- Pointers to struct (C++)
- Errors help (C++)
- HELP: class static function - compile errors (C++)
- coding a complete binary tree with Dev-C++ (C++)
- I GIVE UP, how do I modify this code to call from one form to the other and back (C++)
Other Threads in the C++ Forum
- Previous Thread: confusion in virtual
- Next Thread: math operations
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






