簡體   English   中英

如何將指針數組作為參數傳遞給函數?

[英]How do I pass a pointer to an array of pointers as an argument to a function?

我正在嘗試編寫機器人代碼,而且我的情況令人困惑。 我需要將一個指向對象的數組傳遞給一個類的構造函數。 但是,在將數組傳遞給構造函數之前,我無法填充數組。 為了解決這個問題,我想傳遞一個指向所述數組的指針,並從指針訪問它的元素。 問題是我是C ++的新手,所以我不確定語法。 你能幫助我嗎?

主文件的代碼

class RobotDemo : public SimpleRobot
{
    Joystick stick;
    JoystickOne joyOne;
    Victor *victors [8];
public:
    RobotDemo(void):
        stick(1),
        joyOne(&stick)// these must be initialized in the same order
                // as they are declared above.
                    /*It doesnt seem like I can do anything but initialize things here*/
    {
        /*Populate array with pointers to victors. Will need to update channels*/
        for (int x = 1; x <= 7; x++) {
            victors[x] = new Victor(x);
        }
                    /*And I don't think I can initialize anything here*/
        myRobot.SetExpiration(0.1);
    }

    /**
     * Drive left & right motors for 2 seconds then stop
     */
    void Autonomous(void)
    {
    }

    /**
     * Runs the motors with arcade steering. 
     */
    void OperatorControl(void)
    {
        myRobot.SetSafetyEnabled(true);
        while (IsOperatorControl())
        {
            joyOne.testForActions(); /*Check joystick one for actions*/
            Wait(0.005);                // wait for a motor update time
        }
    }
    /**
     * Runs during test mode
     */
    void Test() {

    }
};

START_ROBOT_CLASS(RobotDemo);

這是JoystickOne類擴展的JoystickInput類的代碼

//the .h
#ifndef JOYSTICKINPUT_H
#define JOYSTICKINPUT_H

#include "WPILib.h"

class JoystickInput {
    public:
        JoystickInput(Joystick*);
        JoystickInput(Joystick*, Victor* [8]);
        Joystick * joystick;
        bool buttons [10];
        Victor** victors [8];
        bool buttonClicked(int id);
        virtual void testForActions();
};
#endif

//and the .cpp
#include "JoystickInput.h"

JoystickInput::JoystickInput(Joystick * joy) {
    joystick = joy;
    for (int x = 0; x < 10; x++) {
        buttons[x] = false;
    }
}
JoystickInput::JoystickInput(Joystick * joy, Victor* vicArray [8]) {
    joystick = joy;
    for (int x = 0; x < 10; x++) {
        buttons[x] = false;
    }
    for (int n = 0; n <=7; n++) {
        *victors[n] = vicArray[n];
    }
}

bool JoystickInput::buttonClicked(int id) {
    if (buttons[id] == false and joystick->GetRawButton(id) == true) {
        buttons[id] = true;
        return true;
    } else if (buttons[id] == true and joystick->GetRawButton(id) == false) {
        buttons[id] = false;
        return false;
    } else {
        return false;
    }
}

void JoystickInput::testForActions() {
}

我要求你們幫我做的是重寫JoystickInput()的構造函數,這樣它還需要一個指針數組(指向Victors),並對數組元素執行方法。 谷歌搜索它沒有任何有用的東西。 我自己研究得更多,但已經過了幾天,我仍然堅持這個。

感謝您的幫助(如果沒有,那么至少閱讀我的帖子)!

你應該可以使用:

JoystickInput(Joystick*, Victor**, int);

並將vicArray傳遞給構造函數。 如果victors可以是長度為8的數組之外的其他任何東西,那么你也應該將長度作為參數傳遞,因為c ++無法從指針中找到數組的長度。

每當類型變得復雜(函數或數組)時,使用typedef:

typedef char char_buffer_type[8]; //char_buffer_type is an array
typedef char (*char_buffer_ptr)[8]; //char_buffer_ptr is a pointer to an array
typedef char (&char_buffer_ref)[8]; //char_buffer_ref is a reference to an array

typedef int main_type(int, char**); //main_type is a "int(int, char**)" function

typedef Victor*(array_of_ptr)[8]; //array_of_ptr is an array of 8 Victor*

此外,您應該將值命名為810

class JoystickInput {
    public:
        static const int victor_count = 8;
        static const int button_count = 10;
        typedef Victor*(array_of_victor_ptr)[victor_count];

        JoystickInput(Joystick*){}
        JoystickInput(Joystick*, array_of_victor_ptr& vicArray);
        bool buttonClicked(int id){return true;}
        virtual void testForActions(){}

        Joystick * joystick;
        bool buttons [button_count];
        array_of_victor_ptr victors; //that's simpler
};

//then pass this one by reference
JoystickInput::JoystickInput(Joystick * joy, array_of_victor_ptr& vicArray) {
    joystick = joy;
    for (int x = 0; x < button_count; x++) {
        buttons[x] = false;
    }
    for (int n = 0; n < victor_count; n++) {
        victors[n] = vicArray[n]; //don't have to dereference here anymore
    }
}

編譯證明 Typedef很精彩。 使用它們。

暫無
暫無

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

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