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

Recommended Answers

All 3 Replies

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.