954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Object compiler errors

Okay, figured that much out but I'm getting compiler errors on my object (which has data members from another object)

void statistics (int rolls[];int rollsize; int rollresults [])
threedice tryerluk;
rollsize=200;
rolls[rollsize];
rollresults[16];

for (int i=0;i<201;i++)
{
   tryerluk.rollem();
   rolls[i]=tryerluk.getsumFaces();
}void statistics (int rolls[];int rollsize; int rollresults [])
threedice tryerluk;
rollsize=200;
rolls[rollsize];
rollresults[16];

for (int i=0;i<201;i++)
{
   tryerluk.rollem();
   rolls[i]=tryerluk.getsumFaces();
}


This gives me a heap of missing type specifier and syntax errors back in the threedice class data members (from class onedie) It says missing type specifier-int assumed C++ does not support default int. What am I missing? Could I have declared my classes wrong?

here is the onedie class header

class onedie
{
	public:
		void rollit();
		int getroll();

	private:
		int result;

};


here is the threedice class header

class threedice
{
	public:
		void rollem();  	  
		int getsumFaces();

	private:

		onedie die1;
		onedie die2;
		onedie die3;

};


Here is the onedie definition (member functions)

#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;
}


and the threedice implementation

#include "onedie.h"
#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()
}
henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

rollsize, rolls, results all need some data type - what are they?

Also, you cannot use the variable rollsize to set the size of the array - it must be a constant value (well, some compilers are letting you do this now, but it's not in C++ standard yet.)

In your for loop, when you get everything else fixed, you'll find it goes one step too far. Use strictly less than the actual array size for the comparison in the loop test i < 200

In the threedice.cpp, don't include onedie.h - you get that already from including threedice.h

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

void statistics (int rolls[];int rollsize; int rollresults [])

You're supposed to use commas to separate the parameters, not semicolons. And if that line is the start of a function where are the { and } at the beginning and end of the function?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

vmanes--my apologies, on my screen they are set as ints, and rollsize is a const int. Thanks for the tip on the loop, I thought since I needed 200 rolls it should go to 201, but I wasn't thinking 0-199. Also thanks for the tip on the include. I'll take care of that post haste.

bah...sorry AD, I got a double paste in there somehow. I'll get that all fixed and see how it turns out. Again, on my screen the braces are there, I must have jacked it up when I pasted it.

And to think, I have to try and write a bank statement tonight too....waaaaahhhhh...lol. I'll try not to bug you guys too much. Lie to me and tell me I'm not as bad as I have been.

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

Okay, it all looks nice and neat now, but I'm still getting those errors.

syntax error missing ; before die1 (etc.)
missing type specifier - int assumed.Note C++ and you get the rest. When I click on them it takes me back to the threedice header where the object onedie is used as a data member (onedie die1)

I'll be home in a few min, maybe I'll log back on to a pleasant surprise...hint hint...lol

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

At least one semicolon seems to be missing ...

int threedice::getsumFaces()
{
         return die1.getroll()+die2.getroll()+die3.getroll() <strong>;</strong> 
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Okay, lets try this again. I have three headers as per the prof:
onedie.h, contains the onedie class declaration

threedice.h, contains the threedice class declaration that also uses onedie as datamember

noclass.h, this holds the free function prototypes

onedie.cpp contains the definition of the onedie class and the member functions for onedie

threedice.cpp does the same as onedie and contains the calls to onedie

noclass. cpp is where I have the implementation of the free functions. It's one of these functions I'm having trouble with. When I try to compile it, I get these errors

error C2146: syntax error : missing ';' before identifier 'die1'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'die2'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'die3'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Now I changed the function implementation like vmanes said, but that gave me even more errors, and upon reading the assignment again, this is how he says to do the function. I'm aware that array sizes are defined with constants, I'm just doin what I'm told here, or trying to. Those errors above, when I click on them take me back to the threedice header.

Mit, I caught that too, thank you

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

Post code. Its impossible for us to evalate the error messages without it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Okay, I'll post it again, assuming you want it all

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

If there's a large amount you could just attach the code. Hit the "Go Advance" button, scroll down to load attachments.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

one die header:

#ifndef ONEDIE_H
#define ONEDIE_H

class onedie
{
	public:
		void rollit();
		int getroll();

	private:
		int result;
};

#endif


threedice header:

#ifndef THREEDICE_H
#define THREEDICE_H

class threedice
{
	public:
		void rollem();  	  
		int getsumFaces();
		
	private:
		onedie die1;
		onedie die2;
		onedie die3;
};

#endif


noclass header:

#ifndef NOCLASS_H
#define NOCLASS_H

void statistics (int rolls[],int rollsize, int allresults[]);  //prototype given by instructor

//display function here

#endif


onedie.cpp

#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

#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

#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?

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

In your THREEDICE.H add the following forward declaration

#define THREEDICE_H
<strong>class onedie;</strong>
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

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    
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

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?

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

I got it to compile without error. Problem seems to be here

#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
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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?

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

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

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;
}
henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

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

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
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

okay, I feel stupid now

henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

you don't need that switch statement at all

for (int i=0;i<rollsize;i++)
{
        allresults[i] = allresults [i]+1;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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:

#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

void displayResults(int allresults[])
	{
		cout << allresults << endl;
	}
henpecked1
Posting Whiz in Training
245 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You