1

我正在尝试编写机器人代码,但情况令人困惑。我需要将指向对象的指针数组传递给类的构造函数。但是,在将数组传递给构造函数之前,我无法填充数组。为了解决这个问题,我想将一个指针传递给所述数组,并从指针访问它的元素。问题是我是 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);

这是 JoystickInput 类的代码,JoystickOne 类扩展了该类

//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)的指针,并对数组元素执行方法。谷歌搜索它没有发现任何有用的东西。我自己会更多地研究它,但已经有几天了,我仍然挂断了这个。

感谢您的帮助(如果不是,那么至少阅读我的帖子)!

4

2 回答 2

2

您应该能够使用:

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

并将 vicArray 传递给构造函数。如果 victors 可以是长度为 8 的数组以外的任何东西,那么您还应该将长度作为参数传递,因为 c++ 无法从指针中找到数组的长度。

于 2013-05-01T23:35:05.813 回答
1

每当类型变得复杂(函数或数组)时,使用 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 很棒。使用它们。

于 2013-05-01T23:41:02.633 回答