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:
我想调用'int Random :: random(int lower,int upper)函数,但是我得到一个问题,'成员函数可能不会在它的类之外重新声明'我也试图提供一个解决方案的形式下列:
'Random m; m.Random()'
'随机m; m.Random()”
which say the following problem 'too few argument in function call'
哪个说下面的问题'函数调用中的参数太少'
Below is the main.cpp file
下面是main.cpp文件
#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
下面是Random.h文件
#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
你能帮忙解决我出错的地方吗?非常感谢所有帮助
3 个解决方案
#1
2
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);
.
首先,调用m.random() - 不存在这样的函数。你需要给它两个int参数。此外,由于它是静态的,你根本不需要Random m;你可以使用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.
这里实际上有两个问题:首先,这不是一个函数调用,它是一个函数声明。函数声明的形式为return_type function_name(arg_type arg_name / * etc. * /);就像你在这里一样。要调用它,您只需要将实际值传递给它,而不是包含返回值 - 这就是它给你的东西。
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.
其次,您需要将结果存储在某处。你的评论表明这应该是CircleArrayOne,但你实际上并没有像你声称的那样填充它。
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
}
#2
2
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.
你既不提供下限也不提供上限。
#3
0
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.
出现问题的是你有一个错误调用函数的语法,它与声明函数的语法不同。如果要调用函数,可以给出函数的名称,然后是parens。在parens之间你提出了你需要提供的任何论据。并且您可能希望使用函数的返回值执行某些操作。现在我并没有真正遵循你想做的事情,但这样的事情可能就是你在寻找的东西。
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.
那么接着是函数Random :: random的名称,在这种情况下接着是参数1和10,你可能想要改变这些值。在这种情况下,我从函数调用中获取返回值,并将其赋值给一个名为result的变量。你可能想要改变它。
This would be covered in any book on C++, might be worth investing in one.
任何有关C ++的书都会涉及这一点,可能值得投资。
#1
2
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);
.
首先,调用m.random() - 不存在这样的函数。你需要给它两个int参数。此外,由于它是静态的,你根本不需要Random m;你可以使用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.
这里实际上有两个问题:首先,这不是一个函数调用,它是一个函数声明。函数声明的形式为return_type function_name(arg_type arg_name / * etc. * /);就像你在这里一样。要调用它,您只需要将实际值传递给它,而不是包含返回值 - 这就是它给你的东西。
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.
其次,您需要将结果存储在某处。你的评论表明这应该是CircleArrayOne,但你实际上并没有像你声称的那样填充它。
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
}
#2
2
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.
你既不提供下限也不提供上限。
#3
0
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.
出现问题的是你有一个错误调用函数的语法,它与声明函数的语法不同。如果要调用函数,可以给出函数的名称,然后是parens。在parens之间你提出了你需要提供的任何论据。并且您可能希望使用函数的返回值执行某些操作。现在我并没有真正遵循你想做的事情,但这样的事情可能就是你在寻找的东西。
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.
那么接着是函数Random :: random的名称,在这种情况下接着是参数1和10,你可能想要改变这些值。在这种情况下,我从函数调用中获取返回值,并将其赋值给一个名为result的变量。你可能想要改变它。
This would be covered in any book on C++, might be worth investing in one.
任何有关C ++的书都会涉及这一点,可能值得投资。