I want to call ‘int Random::random(int lower, int upper) function, however I get a problem saying ‘member function may not be re-declared outside of it class’ also I tried to provide a solution in the form of the following:
‘Random m;
m.Random()’
which say the following problem ‘too few argument in function call’
Below is the main.cpp file
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#include "Circle.h"
#include "Random.h"
int main()
{
Random m;
m.random();
// Array 1, below section is to populate the array with random
// radius number within lower and upper range
int CircleArrayOne [5];
const int NUM = 5;
srand(time(NULL));
for(int x = 0; x < NUM; ++x)
{
int Random::random(int lower, int upper);
}
// output the radius of each circle
cout << "Below is the radius each of the five circles in the second array. " << endl;
// below is to output the radius in the array
for(int i = 0; i < NUM; ++i)
{
cout << CircleArrayOne[i] << endl;
}
system("PAUSE");
return 0;
}
int Random::random(int lower, int upper)
{
cout << "Enter lower number: " << lower << endl;
cout << "Enter upper number: " << upper << endl;
int range = upper - lower + 1;
return (rand() % range + lower);
}
Below is the Random.h file
#pragma once
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random
{
public:
static void initialiseSeed();
// random number initialised
// random number has been initialised to the current time.
static int random(int lower, int upper);
// this function will return a positive random number within a specific lower and
// upper boundary.
};
Can you help with where I’m going wrong please.
All help is much appreciated
There are two issues here.
First, you call
m.random()— no such function exists. You need to give it two int arguments. Also, since it’sstatic, you don’t needRandom mat all; you can just useRandom::random(some_int, some_other_int);.Secondly, you have this:
There are actually two problems here: first, this is not a function call, it’s a function declaraction. Function declarations are of the form
return_type function_name(arg_type arg_name /* etc. */);as you have here. To call it, you need to just pass actual values to it, and not include the return value — that’s what it will give you.Secondly, you need to actually store the result somewhere. Your comments indicate that this is supposed to be
CircleArrayOne, but you don’t actually populate it as you claim to.Try this: