簡體   English   中英

調用成員函數,在類外部進行聲明

[英]Calling member functions, declaring outside of class

我想調用'int Random :: random(int lower,int upper)函數,但是我遇到一個問題,即'成員函數可能無法在它的類外部重新聲明',我也嘗試提供以下形式的解決方案:下列:

'Random 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.
};

請幫我解決我的問題。 非常感謝所有幫助

您的原型:

static int random(int lower, int upper);

您的來電:

Random m;
m.random();

您要么需要提供參數,要么為其提供一些默認值。 同樣,由於該方法是static ,因此不需要實例來調用它。

Random::random(0,100)

足夠。

甚至評論也暗示了這一點:

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

您沒有提供下限或上限。

這里有兩個問題。

首先,您調用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. */); 如你在這里。 要調用它,您只需將實際值傳遞給它,而不包括返回值,這就是它將給您的。

其次,您需要將結果實際存儲在某個地方。 您的評論表明這應該是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
}

出問題的是,您有錯誤地調用函數的語法,這與聲明函數的語法不同。 如果要調用函數,請提供函數名稱,后跟括號。 在括號之間放置您需要提供的任何參數。 您可能想對函數的返回值進行處理。 現在,我並沒有真正按照您的意圖進行操作,但是您可能正在尋找類似的內容。

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

因此,函數名稱為Random::random ,后跟參數1和10,在這種情況下,您可能需要更改這些值。 在這種情況下,我將從函數調用中獲取返回值,並將其分配給名為result的變量。 同樣,您可能想要更改它。

這將在任何有關C ++的書中都有涉及,可能值得投資。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM