【问题标题】:Calling member functions, declaring outside of class调用成员函数,在类外声明
【发布时间】:2012-08-04 22:35:47
【问题描述】:

我想调用 'int Random::random(int lower, int upper) 函数,但是我遇到一个问题,说“成员函数可能不会在它的类之外重新声明”我也试图提供一个解决方案以下形式:

'随机m; m.Random()'

其中说以下问题'函数调用中的参数太少'

下面是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);
}

下面是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.
};

请您帮忙解决我哪里出错了。 非常感谢所有帮助

【问题讨论】:

    标签: c++ visual-studio-2010 function member-functions


    【解决方案1】:

    你的原型:

    static int random(int lower, int upper);
    

    您的电话:

    Random m;
    m.random();
    

    您要么需要提供参数,要么需要为它们提供一些默认值。另外,由于方法是static,所以不需要实例来调用它。

    Random::random(0,100)
    

    够了。

    即使是 cmets 也暗示了这一点:

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

    您既不提供下限也不提供上限。

    【讨论】:

      【解决方案2】:

      这里有两个问题。

      首先,你调用m.random()——不存在这样的函数。你需要给它两个 int 参数。另外,因为它是static,所以你根本不需要Random m;你可以使用Random::random(some_int, some_other_int);

      其次,你有这个:

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

      这里其实有两个问题:第一,这不是函数调用,而是函数声明。函数声明的格式为return_type function_name(arg_type arg_name /* etc. */);,就像您在此处看到的那样。要调用它,您只需将实际值传递给它,而不包括返回值——这就是它会给您的。

      其次,您需要将结果实际存储在某个地方。您的 cmets 表明这应该是 CircleArrayOne,但您实际上并没有像您声称的那样填充它。

      试试这个:

      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
      }
      

      【讨论】:

        【解决方案3】:

        问题是你调用函数的语法错误,它与声明函数的语法不同。如果要调用函数,请给出函数的名称,后跟括号。在括号之间,您可以放置​​您需要提供的任何参数。您可能想对函数的返回值做一些事情。现在,我并没有真正遵循您想要做的事情,但是您可能正在寻找类似的东西。

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

        因此,函数名称Random::random 后跟参数,在这种情况下为 1 和 10,您可能想要更改这些值。在这种情况下,我从函数调用中获取返回值并将其分配给名为result 的变量。同样,您可能想要更改它。

        这将在任何有关 C++ 的书中都有介绍,可能值得投资一本。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-07-31
          • 1970-01-01
          • 1970-01-01
          • 2017-12-17
          • 1970-01-01
          • 1970-01-01
          • 2017-12-08
          相关资源
          最近更新 更多