Question

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

Was it helpful?

Solution

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's static, you don't need Random m at all; you can just use Random::random(some_int, some_other_int);.

Secondly, you have this:

for(int x = 0; x < NUM; ++x)
{
    int Random::random(int lower, int upper);
}

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:

for(int x = 0; x < NUM; ++x)
{
    CircleArrayOne[x] = Random::random(0, 10); // assumed 0 and 10 as the bounds since you didn't specify anywhere; you could use variables here also
}

OTHER TIPS

Your prototype:

static int random(int lower, int upper);

Your call:

Random m;
m.random();

You either need to provide the parameters, or some default values for them. Also, since the method is static, you don't need an instance to call it.

Random::random(0,100)

is enough.

Even the comments hint this:

// this function will return a positive random number within a specific lower and 
// upper boundary.

you provide neither the lower nor upper bound.

What's going wrong is that you've got the syntax for calling a function wrong, it's not the same as the syntax for declaring a function. If you want to call a function you give the name of the function, followed by parens. And between the parens you put any arguments you need to supply. And you might like to do something with the return value from the function. Now I don't really follow what you are trying to do, but something like this might be what you are looking for.

int result = Random::random(1, 10);

So that then name of the function Random::random, followed by the arguments, 1 and 10 in this case, you'll probably want to change those values. And in this case I'm taking the return value from the function call and assigning it to a variable called result. Again you might want to change that.

This would be covered in any book on C++, might be worth investing in one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top