I can't figure out why I am getting this error. I have tried #include several things and I have tried changing the location of srand.

#include <iostream.h>
#include <ctime>



class Horse {


    int position;
    public:
    Horse();                    //Horse constructor??? Per UML
    int random;
    srand( time(0));
    void advance(){
        random = rand() % 2;
        cout << random;
    }//end advance function

    int getPosition(int y){

    return y;
    }//end getPosition function


};//end Horse class

Edited 3 Years Ago by Nick Evan: Fixed formatting

I haven't dealt with classes yet, but i would assume you'd have to place it inside the function. Have you tried ?

you can not put function calls in class declaration unless they are inside inline code. Move that srand() line to main()

int main()
{
   srand(time(0));
}

Depending on the compiler you are using you may have to typecase time() to unsigned int instead of time_t.

A class is made up of functions and variables so you can not randomly throw in a function call like srand() unless it is within one of it's own functions. I would call srand() at the top of your main() function.

#include <iostream> //use iostream not iostream.h (.h is the c version)
#include <ctime>
using namespace std;

class Horse
{
	int position;
	int random;

	public:

	Horse(){}; //note the two curly brackets (this is the default constructor that is made if you do not include a constructor)

	void advance()
	{
		random = rand() % 2;
		cout << random;
	}

	int getPosition( int y )
	{
		return y;
	}

};

int main()
{
	srand(time(0));//seed your random here.
	Horse test;
	test.advance();

	return 0;
}
This article has been dead for over six months. Start a new discussion instead.