【问题标题】:[AccelStepper][PlatformIO][mbed] Request for member '' in '', which is of non-class type[AccelStepper][PlatformIO][mbed] 请求''中的成员'',它是非类类型
【发布时间】:2019-10-01 09:13:32
【问题描述】:

所以我一直在尝试创建 3 个“accelstepper”对象。 This is a screenshot of my code in case the code section doesn't appear。另外,this is a screenshot of the file "stepper_directory.h"

#include <mbed.h>
#include "stepper_directory.h"

//Include accelstepper library
#include <AccelStepper.h>

//Create accelstepper object for the Z-Axis actuator
AccelStepper zaxis(uint8_t interface = AccelStepper::DRIVER, uint8_t zstep = ZSTEP, uint8_t zdir = ZDIR);

//Create accelstepper object for the theta axis actuator
AccelStepper taxis(uint8_t interface = AccelStepper::DRIVER, uint8_t tstep = TSTEP, uint8_t tdir = TDIR);

//Create accelstepper object for the magnet actuator
AccelStepper maxis(uint8_t interface = AccelStepper::DRIVER, uint8_t mstep = MSTEP, uint8_t mdir = MDIR);

这是我用过的头文件“stepper_directory.h”

#ifndef _STEPPER_DIRECTORY
#define  _STEPPER_DIRECTORY

#include "PinNames.h"

//Pin Definitions
#define ZSTEP   PA_7
#define ZDIR    PA_0

#define TSTEP   PA_8
#define TDIR    PA_1

#define MSTEP   PA_9
#define MDIR    PA_2

我尝试在 main.cpp 的主代码中设置一个步进器,如下所示:

int main() {
    // put your setup code here, to run once:
    zaxis.setMaxSpeed(188000);

    while(1) {
    // put your main code here, to run repeatedly:
    }
}

但是 platformIO 编译器抛出了这行代码:

src\main.cpp: In function 'int main()':
src\main.cpp:17:7: error: request for member 'setMaxSpeed' in 'zaxis', which is of non-class type 'AccelStepper(uint8_t, uint8_t, uint8_t)
{aka AccelStepper(unsigned char, unsigned char, unsigned char)}'
 zaxis.setMaxSpeed(188000);
       ^~~~~~~~~~~
*** [.pio\build\nucleo_f410rb\src\main.o] Error 1

我一直试图搜索我的对象实例化有什么问题,但无济于事。如果有人能解释这有什么问题,我将不胜感激。 This is a screenshot of the error in question

【问题讨论】:

    标签: class mbed platformio


    【解决方案1】:

    问题就在这里。

    //Create accelstepper object for the Z-Axis actuator
    AccelStepper zaxis(uint8_t interface = AccelStepper::DRIVER, uint8_t zstep = ZSTEP, uint8_t zdir = ZDIR);
    

    这是一个函数声明。它接受三个参数并返回AccelStepper。您不能使用这行代码初始化AccelStepper 的实例。

    我假设AccelStepper 的构造函数是这样的:

    AccelStepper AccelStepper(uint8_t interface, uint8_t zstep, uint8_t zdir);
    
    

    你可以这样初始化你的实例:

    AccelStepper zaxis(AccelStepper::DRIVER, ZSTEP, ZDIR);
    
    

    【讨论】:

    • 您好,奥田健太郎,感谢您的回复。只是为了澄清我的理解,即使我打算将其作为对象实例化,我的原始代码行仍表现为函数的原因是因为我对参数的措辞方式?
    • 声明、定义和调用不同。您的原始代码是一个函数声明。它不会初始化实例,因为您没有调用构造函数。此链接可能会有所帮助。 studytonight.com/cpp/functions-in-cpp.php
    • 感谢您的帮助!我听从了你的建议,效果很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多